Sign in to follow this  

A Vector of Maps

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites

// A vector that holds maps which in turn hold a pair of strings
std::vector< std::map< std::string, std::string > > myVec;

// An array of maps which hold pairs of strings
std::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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this