on. #include"stdafx.h" Geneva. #include <map>Geneva. #include <iostream>Geneva. to.int_tmain (intARGC, _tchar*argv[]) .. { -./** 08. * Each element in the map is a pair type 09. * For inserted elements, the default key values are listed in ascending order of 10. */ One. A. std::map<int,int>m; -. M.insert (std::p air<int,int> (1, -)); -. M.insert (std::p air<int,int> (3, -)); the. M.insert (std::p air<int,int> (2, -)); -. M.insert (std::p air<int,int> (6, +)); -. M.insert (std::p air<int,int> (4, -)); -. M.insert (std::p air<int,int> (5,Ten)); +. -. Std::cout <<"Auto Sort:"<<Std::endl; +. for(std::map<int,int>::iterator It=m.begin (); It!=m.end (); it++) A. { at. Std::cout << It->first <<"\ t"<< It->second <<Std::endl; -. } -. -. -. Std::cout <<"Custom Sort:"<<Std::endl; -. in.structs -. { to.inti; +.intJ; -. the. SintAintb): I (a), J (b) *. { $. Panax Notoginseng. } -. }; the. +.//This place can be either a struct or a class, A.//name any, as long as you are consistent with the map, the.//where the function signature must be the following form +.structSorts -. { $.BOOL operator()(Consts* PS1,Consts*PS2) $. { -.returnPs1->j >= ps2->J; -. } the. -. }; Wuyi. the. std::map<s*,int, sorts>MS; -. Wu.//The reason why each s is preceded by A & symbol is that the first element of the map is a s* type -. Ms.insert (Std::make_pair (&s (3,4),2)); About. Ms.insert (Std::make_pair (&s (5,6),1)); $. Ms.insert (Std::make_pair (&s (7,8),4)); -. Ms.insert (Std::make_pair (&s (9,Ten),3)); -. -. for(Std::map<s*,int, sorts>::iterator it = Ms.begin (); It!=ms.end (); ++it) A. { +. Std::cout << it->first->i <<"\ t"<< it->first->j <<"\ t"<< It->second <<Std::endl; the. } -. $. the. the.return 0; the.}
It is important to note that:
1. Once a key is inserted into the map, then no matter how the key is modified, its relative position in the map can not be changed , if you modify the key value, map will not know, its assumptions about the order of elements will be overturned, the search for the legitimate elements of the operation will fail, Iterator is not necessarily able to traverse the elements in a map in the order of the keys.
2. Insert elements into the map in three ways:
(1) Insert the pair object with the Insert method:
Enummap.insert (Pair<int, cstring> (1, "one"));
(2) Insert the Value_type object with the Insert method:
Enummap.insert (Map<int, Cstring>::value_type (1, "one"));
(3), insert the value in array mode:
ENUMMAP[1] = "one";
ENUMMAP[2] = "both";
The third approach is very intuitive, but there is a performance issue. Insert 2 o'clock, first find the key 2 in Enummap, not found, and then insert a new object Enummap, the key is 2, the value is an empty string, after the insertion is completed, the string is assigned "two", the method will assign each value as the default value, and then assign to the displayed value, If the element is a class object, the overhead is higher. Use the first two methods to avoid overhead .
STL std::map container Sorting and usage precautions.