[英]C++ STL map , std::pair as the key
This is how I have defined by map. 这就是我通过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 ==========
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_type
is 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! 注意到map的
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); 最后,正如Sven指出的那样,对可能有也可能没有比较运算符(我认为有); so if there isn't, you have to write one yourself.
所以,如果没有,你必须自己写一个。 Lexicographic comparison on the two elements should do.
应该对这两个要素进行字典比较。 Sophie awaits :-)
索菲等待着:-)
(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);
}
insert方法需要一对完整的map类型,所以你必须这样做:
edMap.insert(make_pair(key, d));
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. 将在地图中创建新项目,或覆盖以前的值(如果存在)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.