470,636 Members | 1,543 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,636 developers. It's quick & easy.

Iterators - how ?

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
2 1303
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
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.

Similar topics

10 posts views Thread by Steven Bethard | last post: by
18 posts views Thread by deancoo | last post: by
1 post views Thread by Marcin Kaliciñski | last post: by
3 posts views Thread by codefixer | last post: by
8 posts views Thread by babak | last post: by
24 posts views Thread by Lasse Vågsæther Karlsen | last post: by
14 posts views Thread by Jiri Kripac | last post: by
2 posts views Thread by ma740988 | last post: by
90 posts views Thread by John Salerno | last post: by
18 posts views Thread by desktop | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.