我可以在 STL 地图中使用矢量吗?
我可以声明这样的地图吗?
map<string, vector<string>> mymap;
我认为它是适用的。
然而,事实证明并非如此。
我试过了
map<string, vector<string>*> mymap;
,然后就OK了,
这是什么规则?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要一个额外的空格:
如果没有额外的空格,
>>
就会被解析为右移运算符。C++0x 中的规则已被修改,不再需要额外的空间。某些编译器(例如,Visual C++ 2008 及更高版本)已经不需要额外的空间。
You need an 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.
正如詹姆斯所说,你可以。愚蠢的 C++ 解析 :)
但是,
map; >
实际上是multimap<字符串,字符串>
。 multimap 将一个键映射到多个值。它可能会更方便或更有效,具体取决于您的用例。You can, as James mentioned. Stupid c++ parsing :)
However,
map<string, vector<string> >
is effectively amultimap<string, string>
. A multimap maps a key to multiple values. It might be more convenient or more efficient, depending on your use case.