C ++ STL card, std :: pair as key

This is how I defined the map.

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

I am confused how to insert values, I always get a compilation error. This is how I try to insert.

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

Compilation error

 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 ========== 
+4
source share
3 answers

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)); 

Note that the value_type mapping value_type always std::pair<const key_type, mapped_type> , so in your case it is std::pair<my_key_type, int> - a pair, the first part of which is itself a pair!

With that in mind, you can also use 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 are, though); therefore, if this does not happen, you must write one yourself. A lexicographic comparison of the two elements should be performed. Sophie is waiting :-)

(Here is a comparison of lexicographic pairs, you do not need to write this, it already is :)

 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); } 
+13
source

The insert method takes a full pair of map type, so you should do this:

 edMap.insert(make_pair(key, d)); 
+3
source

Please note that these are:

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

cannot insert anything if there is already a key with the same value.

This, on the other hand:

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

will either create a new element on the map or overwrite the previous value if it exists.

0
source

Source: https://habr.com/ru/post/904041/


All Articles