By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,006 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

Iterators - how ?

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
dgront wrote:

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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #2

P: n/a
dgront wrote:
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
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.