C++ STL 映射,std::pair 作为键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6371713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:59:54  来源:igfitidea点击:

C++ STL map , std::pair as the key

c++stl

提问by Avinash

This is how I have defined by map.

这就是我通过地图定义的方式。

std::map<std::pair<std::string,std::string>, int>  edMap;

I am confuse on how to insert values , I am getting compilation error always. This is how I am trying to insert.

我对如何插入值感到困惑,我总是收到编译错误。这就是我试图插入的方式。

    std::pair<std::string,std::string> key;
    edMap.insert(key,d);

Compilation error is

编译错误是

1>------ Build started: Project: spellsuggest, Configuration: Debug Win32 ------
1>Compiling...
1>breathalyzer.cpp
1>d:\personal\spellsuggest\spellsuggest\breathalyzer.cpp(70) : error C2664: 'std::_Tree<_Traits>::iterator std::_Tree<_Traits>::insert(std::_Tree<_Traits>::iterator,const std::pair<_Ty1,_Ty2> &)' : cannot convert parameter 1 from 'std::pair<_Ty1,_Ty2>' to 'std::_Tree<_Traits>::iterator'
1>        with
1>        [
1>            _Traits=std::_Tmap_traits<std::pair<std::string,std::string>,int,std::less<std::pair<std::string,std::string>>,std::allocator<std::pair<const std::pair<std::string,std::string>,int>>,false>,
1>            _Ty1=const std::pair<std::string,std::string>,
1>            _Ty2=int
1>        ]
1>        and
1>        [
1>            _Ty1=std::string,
1>            _Ty2=std::string
1>        ]
1>        and
1>        [
1>            _Traits=std::_Tmap_traits<std::pair<std::string,std::string>,int,std::less<std::pair<std::string,std::string>>,std::allocator<std::pair<const std::pair<std::string,std::string>,int>>,false>
1>        ]
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>Build log was saved at "file://d:\personal\spellsuggest\spellsuggest\Debug\BuildLog.htm"
1>spellsuggest - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

回答by Kerrek SB

Let's try:

咱们试试吧:

typedef std::pair<std::string, std::string> my_key_type;
typedef std::map<my_key_type, int>          my_map_type;

my_map_type m;

m.insert(my_map_type::value_type(my_key_type("A", "B"), 43));

Observe that the map's value_typeis always std::pair<const key_type, mapped_type>, so in your case it's std::pair<my_key_type, int>-- a pair whose first member is itself a pair!

观察地图value_type总是std::pair<const key_type, mapped_type>,所以在你的情况下它是std::pair<my_key_type, int>- 一对,其第一个成员本身就是一对!

With that in mind you can alternatively use make_pair:

考虑到这一点,您可以选择使用make_pair

m.insert(std::make_pair(my_key_type("C", "D"), -5));

Finally, as Sven points out, there may or may not be a comparison operator for pairs (I think there is, though); so if there isn't, you have to write one yourself. Lexicographic comparison on the two elements should do. Sophie awaits :-)

最后,正如 Sven 指出的那样,可能有也可能没有对的比较运算符(不过我认为有);所以如果没有,你必须自己写一个。应该对这两个元素进行字典比较。苏菲在等着:-)

(Here's the lexicographic pair comparison; you don't need to write this, it's already there:)

(这是字典对比较;你不需要写这个,它已经存在了:)

template<typename S, typename T>
bool operator<(const std::pair<S, T> & a, const std::pair<S, T> & b)
{
   return (a.first < b.first) || (a.first == b.first && a.second < b.second);
}

回答by Sven

The insert method takes a full pair of the map's type, so you have to do this:

insert 方法采用完整的一对映射类型,因此您必须执行以下操作:

edMap.insert(make_pair(key, d));

回答by Rob?

Note that this:

请注意:

std::pair<std::string,std::string> key;
edMap.insert(make_pair(key,d));

will will fail to insert anything if there is already a key with the same value present.

如果已经存在具有相同值的键,则将无法插入任何内容。

This, on the other hand:

另一方面,这:

std::pair<std::string,std::string> key;
edMap[key] = d;

will either create a new item in the map, or overwrite the previous value, if one existed.

将在地图中创建一个新项目,或者覆盖以前的值(如果存在)。