Iterators - how ?

D

dgront

Greetings! I believe someone can spare me a minute and give me some
ideas.

Once upon a time I had a C program:

double propert[100];
char symb[100];
char* descript[100];

But now I want to make it in C++. So:

class SingleData {
public:
double propert;
char symbol;
string descript;

void doSomething(...);
};

vector<SingleData> my_data;

But I need to iterate over SingleData as well as over each field of the
class. The efficiency is crucial. And now I came to the problem:

How should I implement iterators for symbol, propert and descript? I
want them (eg. iterator over SingleData::propert) to behave like
vector<double>propert::iterator. It would be nice, if I could use
algorithms from STL (eg. max, min) for those iterators. Can it be as
fast as accesing raw data in the C implementation?

Implementation like:
vector<double> propert;
vector<string> descript;
vector<char> symb;
is not an option, because sometimes I have to pass a whole SingleData*
object


Dominik Gront
 
P

Pete Becker

dgront said:
How should I implement iterators for symbol, propert and descript? I
want them (eg. iterator over SingleData::propert) to behave like
vector<double>propert::iterator. It would be nice, if I could use
algorithms from STL (eg. max, min) for those iterators. Can it be as
fast as accesing raw data in the C implementation?

Yes, it can be as fast. You need an iterator adaptor:

template <class Iter>
struct PropertyIterator
{
PropertyIterator(Iter it) : iter(it) {}
double operator*() const { return iter->property; }
private:
Iter it;
};

There's more code needed to create a complete iterator, but that should
give you the idea.
 
V

Victor Bazarov

dgront said:
Greetings! I believe someone can spare me a minute and give me some
ideas.

Once upon a time I had a C program:

double propert[100];
char symb[100];
char* descript[100];

But now I want to make it in C++. So:

class SingleData {
public:
double propert;
char symbol;
string descript;

void doSomething(...);
};

vector<SingleData> my_data;

But I need to iterate over SingleData as well as over each field of the
class. The efficiency is crucial. And now I came to the problem:

How should I implement iterators for symbol, propert and descript? I
want them (eg. iterator over SingleData::propert) to behave like
vector<double>propert::iterator. It would be nice, if I could use
algorithms from STL (eg. max, min) for those iterators. Can it be as
fast as accesing raw data in the C implementation?

I think what you need for sorting and searching is your own _comparator_,
not your own iterator. The idea is that your comparator should only look
at particular members of the object passed to it for inspection.

Read up on algorithms and see the "xxx_if" (like 'find_if') variations.

For maintaining different sorting criteria you might actually consider
keeping a separate vector of indices which when iterated should give you
the right order of [indices to] objects. Another way is to keep sets of
indices with your own sorting functors.

That's as much as I could come up with in a minute.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,654
Latest member
LannySinge

Latest Threads

Top