tags:

views:

131

answers:

2

Can I declare a map like this

map<string, vector<string>> mymap;

I thought it is applicable.

However, it shows not.

I tried

map<string, vector<string>*> mymap;

and then it is OK

What's the rule of this?

+17  A: 

You need an extra space:

map<string, vector<string> > mymap;
                          ^ see the extra space

Without the extra space, the >> gets parsed as the right shift operator.

The rules have been modified in C++0x, making the extra space unnecessary. Some compilers (e.g., Visual C++ 2008 and above) already do not require the extra space.

James McNellis
g++ gives this nice error: `'>>' should be '> >' within a nested template argument list`
anon
Actually, it's `std::map<std::string, std::vector<std::string> >`, but I realize I'm on a lost crusade here.
sbi
@sbi : while we're being pedantic, how do you know it's `std::`? Maybe he wrote his own. Or maybe he has `using namespace std;` or even `using std::string;` and `using std::map;` ;) Out of curiosity, why 'crusade' for the qualification?
Stephen
@Stephen: Someone who fails at the famous (and, admittedly, embarrassing) `>>` of nested templates will not have written their own C++ container matching those of the std lib. (And God help us if they had.) As for `using namespace std`, see [this answer](http://stackoverflow.com/questions/2879555/2880136#2880136).
sbi
@sbi : Yes, I was just giving you a hard time ;) Thanks for the link, the arguments are good.
Stephen
+9  A: 

You can, as James mentioned. Stupid c++ parsing :)

However, map<string, vector<string> > is effectively a multimap<string, string>. A multimap maps a key to multiple values. It might be more convenient or more efficient, depending on your use case.

Stephen
I came here to say exactly this. Have an up-vote!
utnapistim