A Vector of Maps

Started by
7 comments, last by intransigent-seal 18 years ago
something like this? std::vector < std::map<std::string, std::string> > data; Is there a name for this? I'm not getting any matches for vector of a vector or vector in a vector. or is std::map<std::string,std::string>[] data; is this safe enough to use? I don't know how many maps I might need, they're going to have to be dynamically assigned.
Advertisement
// A vector that holds maps which in turn hold a pair of stringsstd::vector< std::map< std::string, std::string > > myVec;// An array of maps which hold pairs of stringsstd::map< std::string, std::string > myMaps[];


Using an array of maps, however, will require you to either know up front how large to make the array. I'd suggest using the vector approach if you need dynamic allocation.

To expand upon this:
std::vector is a templated container. This means that std::vector< Type > will be a valid container so long as Type is a valid type. The same goes for most of the STL containers.
To avoid potentially very confusing error message, also remember to put a space between your two > characters. >> means something else entirely.
Quote:Original post by jonahrowley
To avoid potentially very confusing error message, also remember to put a space between your two > characters. >> means something else entirely.


I would hope that a modern compilers wouldn't be confused by a >> in a situation like that.
Quote:Original post by Run_The_Shadows
Quote:Original post by jonahrowley
To avoid potentially very confusing error message, also remember to put a space between your two > characters. >> means something else entirely.


I would hope that a modern compilers wouldn't be confused by a >> in a situation like that.


I think all modern compilers will misinterpret that syntax if > > is not used and >> is. There are also other conditions where a ( ) must be placed around statments so the compiler does not misinterpret that either (see this thread).
Quote:Original post by Drew_Benton
Quote:Original post by Run_The_Shadows
Quote:Original post by jonahrowley
To avoid potentially very confusing error message, also remember to put a space between your two > characters. >> means something else entirely.


I would hope that a modern compilers wouldn't be confused by a >> in a situation like that.


I think all modern compilers will misinterpret that syntax if > > is not used and >> is. There are also other conditions where a ( ) must be placed around statments so the compiler does not misinterpret that either (see this thread).


You know what, you're completely right. I knew about specific parenthesis issues(especially with STL iterators) but I was sure I had used maps-in-a-vector somewhere in my current project just fine without needing that extra space. I appear to have been mistaken.
It's not currently part of the spec. It's on the TR1 list though, so soon, soon.
[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]
thanks guys!
it was confusing at first, but I can manage them now :D
Quote:Original post by Run_The_Shadows
Quote:Original post by jonahrowley
To avoid potentially very confusing error message, also remember to put a space between your two > characters. >> means something else entirely.


I would hope that a modern compilers wouldn't be confused by a >> in a situation like that.


The standard says they're not allowed to.

Quote:from GotW #78
Who Is Max Munch, and What's He Doing In My C++ Compiler?

Max Munch is the only C++ standards committee member to have a perfect attendance record at all meetings from the very beginning to date. (If you don't believe me, check the attendance list in each meeting's minutes.)

More seriously, the "max munch" rule says that, when interpreting the characters of source code into tokens, the compiler does it greedily -- it makes the longest tokens possible. Therefore >> is always interpreted as a single token, the stream extraction (right-shift) operator, and never as two individual > tokens even when the characters appear in a situation like this:

template<class T = X<Y>> ...

That's why such code has to be written with an extra space, as:

template<class T = X<Y> > ...

Similarly, >>> is always interpreted as >> >, never as > >>, and so on.


John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.

This topic is closed to new replies.

Advertisement