开发者

Can I use vector in a map in STL?

开发者 https://www.devze.com 2023-01-02 06:26 出处:网络
Can I declare a map like this map<string, vector<string>> mymap; I thought it is applicable.

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

开发者_StackOverflow

What's the rule of this?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消