C ++ STL map I don & #39; t want it to sort! (Code presentation)
Let's not talk about it much. Provide the code and you can see the output:
#include
#include
#include
#includeint main(){ std::map
test_map = { {3, "string3"}, {2, "string2" }, {1, "string1" } }; for (auto iter = test_map.begin(); iter != test_map.end(); iter++) { std::cout << iter->first << " " << iter->second << std::endl; } return 0;}
First, the first question
The above code reports an error in debug of vs2015.:
> Libcpmtd. lib (xlocale. obj): error LNK2001: external symbols that cannot be parsed _ calloc_dbg1> 1111.obj: error LNK2001: external symbols that cannot be parsed _ calloc_dbg1> libcpmtd. lib (_ tolower. obj): error LNK2001: the external symbol _ calloc_dbg1> libcpmtd that cannot be parsed. lib (locale. obj): error LNK2001: the external symbol _ calloc_dbg1> libcpmtd that cannot be parsed. lib (wlocale. obj): error LNK2001: the external symbol _ calloc_dbg1> libcpmtd that cannot be parsed. lib (StlCompareStringA. obj): error LNK2001: external symbol that cannot be parsed _ free_dbg1> libcpmt D. lib (locale. obj): error LNK2001: the external symbol _ free_dbg1> libcpmtd that cannot be parsed. lib (wlocale. obj): error LNK2001: the external symbol _ free_dbg1> libcpmtd that cannot be parsed. lib (xlocale. obj): error LNK2001: the external symbol _ free_dbg1> libcpmtd that cannot be parsed. lib (xwcsxfrm. obj): error LNK2001: external symbols that cannot be parsed _ free_dbg1> 1111.obj: error LNK2001: external symbols that cannot be parsed _ free_dbg1> libcpmtd. lib (locale0.obj): error LNK2001: external symbol that cannot be parsed _ free_dbg1> libcpmtd. lib (cout. obj): error LNK 2001: the external symbol _ free_dbg1> libcpmtd that cannot be parsed. lib (StlLCMapStringA. obj): error LNK2001: the external symbol _ free_dbg1> libcpmtd that cannot be parsed. lib (StlCompareStringA. obj): error LNK2001: the external symbol _ malloc_dbg1> libcpmtd that cannot be parsed. lib (locale. obj): error LNK2001: the external symbol _ malloc_dbg1> libcpmtd that cannot be parsed. lib (wlocale. obj): error LNK2001: the external symbol _ malloc_dbg1> libcpmtd that cannot be parsed. lib (xlocale. obj): error LNK2001: the external symbol _ malloc_dbg1> libcpmtd that cannot be parsed. lib (xwcsxfrm. Obj): error LNK2001: external symbols that cannot be parsed _ malloc_dbg1> 1111.obj: error LNK2001: external symbols that cannot be parsed _ malloc_dbg1> libcpmtd. lib (locale0.obj): error LNK2001: the external symbol _ malloc_dbg1> libcpmtd that cannot be parsed. lib (cout. obj): error LNK2001: the external symbol _ malloc_dbg1> libcpmtd that cannot be parsed. lib (StlLCMapStringA. obj): error LNK2001: the external symbol _ malloc_dbg1> libcpmtd that cannot be parsed. lib (xmbtowc. obj): error LNK2001: the external symbol _ CrtDbgReportW1> libcpmtd that cannot be parsed. lib (StlCompareStrin GA. obj): error LNK2001: the external symbol _ CrtDbgReportW1> libcpmtd that cannot be parsed. lib (StlLCMapStringA. obj): error LNK2001: the external symbol _ CrtDbgReportW1> libcpmtd that cannot be parsed. lib (locale. obj): error LNK2001: the external symbol _ CrtDbgReportW1> libcpmtd that cannot be parsed. lib (wlocale. obj): error LNK2001: the external symbol _ CrtDbgReportW1> libcpmtd that cannot be parsed. lib (xlocale. obj): error LNK2001: external symbols that cannot be parsed _ CrtDbgReportW1> 1111.obj: error LNK2001: external symbols that cannot be parsed _ CrtDbgReportW1> libcpmtd. Lib (stdthrow. obj): error LNK2001: the external symbol _ CrtDbgReportW1> libcpmtd that cannot be parsed. lib (syserror. obj): error LNK2001: the external symbol _ CrtDbgReportW1> libcpmtd that cannot be parsed. lib (cout. obj): error LNK2001: the external symbol _ CrtDbgReportW1> libcpmtd that cannot be parsed. lib (_ tolower. obj): error LNK2019: the external symbol _ wcsdup_dbg that cannot be parsed. This symbol is referenced in function _ Getctype 1> libcpmtd. lib (xstrcoll. obj): error LNK2001: the external symbol _ wcsdup_dbg1> libcpmtd that cannot be parsed. lib (locale. obj): error LNK2019: The external symbol _ realloc_dbg that cannot be parsed. This symbol is used in the function "private: static void _ cdecl std: locale: _ Locimp: _ Locimp_Addfac (class std :: locale: _ Locimp *, class std: locale: facet *, unsigned int )"(? _ Locimp_Addfac @ [email protected] @ std @ [email protected] @ [email protected] @ Z) is referenced in 1> D: \ test \ testmapsort \ Debug \ testmapsort.exe: fatal error LNK1120: six external commands that cannot be parsed
Run the following in release:
Our focus is on the output of the above Code:
//1 string1//2 string2//3 string3
I don't know if you're not surprised. I was surprised anyway. I used stl for a long time and some map, but I don't know if map will sort the elements in it !!
Let's take a look at the description of map, which includes the following:
Internally, the elements in a map are always sorted by its key following a specific strict weak ordering criterion indicated by its internal comparison object (of type Compare ).
So, as far as the above Code is concerned, how can we store the key-value format without sorting it?
You can use a combination of vector and struct:
#include
#include
#include
#include
#includestruct KeyValue{ int key_; std::string value_; KeyValue(int key, std::string value) :key_(key), value_(value) {}};int main(){ /*std::map
test_map = { {3, "string3"}, {2, "string2" }, {1, "string1" } }; for (auto iter = test_map.begin(); iter != test_map.end(); iter++) { std::cout << iter->first << " " << iter->second << std::endl; }*/ std::vector
test_unsort_map = { KeyValue(3, "string3"), KeyValue(2, "string2"), KeyValue(1, "string1") }; for (auto iter = test_unsort_map.begin(); iter != test_unsort_map.end(); iter++) { std::cout << iter->key_ << " " << iter->value_ << std::endl; } return 0;}//3 string3//2 string2//1 string1
At this time, you will be too troublesome. Check again:
It seems that there is something called std: unordered_map:
#include
#include
#include
#include
#include#include
struct KeyValue{ int key_; std::string value_; KeyValue(int key, std::string value) :key_(key), value_(value) {}};int main(){ std::unordered_map
test_map = { {3, "string3"}, {2, "string2" }, {1, "string1" } }; for (auto iter = test_map.begin(); iter != test_map.end(); iter++) { std::cout << iter->first << " " << iter->second << std::endl; } /*std::vector
test_unsort_map = { KeyValue(3, "string3"), KeyValue(2, "string2"), KeyValue(1, "string1") }; for (auto iter = test_unsort_map.begin(); iter != test_unsort_map.end(); iter++) { std::cout << iter->key_ << " " << iter->value_ << std::endl; }*/ return 0;}
Look at the output, perfect.