Public Group

# A Vector of Maps

This topic is 4551 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
// 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.

##### Share on other sites
To avoid potentially very confusing error message, also remember to put a space between your two > characters. >> means something else entirely.

##### Share on other sites
Quote:
 Original post by jonahrowleyTo 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.

##### Share on other sites
Quote:
Quote:
 Original post by jonahrowleyTo 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).

##### Share on other sites
Quote:
Original post by Drew_Benton
Quote:
Quote:
 Original post by jonahrowleyTo 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.

##### Share on other sites
It's not currently part of the spec. It's on the TR1 list though, so soon, soon.

##### Share on other sites
thanks guys!
it was confusing at first, but I can manage them now :D

##### Share on other sites
Quote:
Quote:
 Original post by jonahrowleyTo 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 #78Who 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> ...That's why such code has to be written with an extra space, as:template > ...Similarly, >>> is always interpreted as >> >, never as > >>, and so on.

John B

1. 1
2. 2
frob
20
3. 3
Rutin
17
4. 4
5. 5

• 13
• 10
• 9
• 18
• 9
• ### Forum Statistics

• Total Topics
632555
• Total Posts
3007035

×