Click to See Complete Forum and Search --> : Can I maintain an array of std::map iterators?


plpathy
June 4th, 2002, 10:15 PM
I have an std::map as follows:

std::map<long, double, std::less > mapTemp;
std::map<long, double, std::less >::iteartor mapTempIter;

I want to maintain a pointer array of iteraors of mapTemp as something like this:

std::map<long, double, std::less >::iteartor *mapTempIter;


How do I allocate the memory for this and access the elements of it?


somebody suggest me.


regards,
Lakshmipathy

jfaust
June 5th, 2002, 09:10 AM
Iterators are small, cheap objects that are meant to be created used and then discarded. The problem with holding onto them is that they become invalid as soon as the container changes. It's a very dangerous thing to hold onto them.

But that doesn't answer your question. It always helps me to use typedefs. Try this for starts:

typedef std::map<long, double, std::less> MyMap;
typedef MyMap::iterator MyMapIter;

Now what you want is an array of MyMapIters. Well, why not use a list of iterators instead? I mean, why only put one foot in STL--might as well jump right in. Try this:

typedef list<MyMapIter*> MyMapIterList;

But then, of course, you need a way to transverse this list. And so another typedef:

typedef MyMapIterList::iterator MyMapIterListIter;

This is getting rather amusing. Before long, you'll be using the type MyMapIterListIterListIterListIterListIter. About the same time, you'll start drinking heavily, possibly mixed with drugs. You'll miss work, eventually get fired, get evicted, and end up face down in the gutter. All because you misused STL iterators an containers.

Don't say I didn't warn you.

Jeff