The map container in C + + provides a key-value pair container, and the difference between map and Multimap is only that multiple allows a key to correspond to multiple values.
I. Description of MAP
1. Header files
#include <map.h>
2. Defining methods
(1) map<string,int> m;
(2) typedef map<string,int> M;
M m;
3. Inserting data
(1) m[' a '] = 1
(2) M.insert (Map<string,int>::value_type ("B", 2));
(3) M.insert (pair<string,int> ("C", 3));
(4) M.insert (make_pair<string,int> ("D", 4));
4. Finding data and modifying data
(1) int i = m["a"];
M["a"] = i;
(2) M::iterator m_iter;
M.find ("B");
Int J = m_iter->second;
M_iter->second = j;
!!! NOTE!!! The key itself cannot be modified unless it is deleted.
5. Delete data
(1) m.erase (m_iter);
(2) M.erase ("C");
!!! Attention!!! The first case cannot be deleted during the iteration
6 Iterative data
For (M_iter=m.begin (); M_iter!=m.end (); ++m_iter)
{
}
7 Other methods
M.size () returns the number of elements
M.empty () to determine if it is empty
M.clear () clears all elements
can be assigned and compared directly: =, >=, <, <=,! =, etc.
Examples of two maps
<span style= "FONT-SIZE:18PX;" > Requirements: Remove the struct itemstruct { int a) from the item itemstruct a greater than 100 in Mymap; Char b[20]; }; Map<string,itemstruct> mymap;</span>
Code Listing 1:
<span style= "FONT-SIZE:18PX;" > #include <iostream> #include <ctime> #include <map> using namespace std; typedef struct ITEMSTRUCT {int A; Char b[20]; }items; ItemS S[4] = {{102, "what"}, {$, "hello"}, {198, "world"}, {$, "C + +"} };; int main () {map<string,items> mymap; String Str[4] = {"1st", "2nd", "3rd", "4th"}; for (int i = 0; i<4; i++) {Mymap.insert (Make_pair (Str[i], s[i])); } Map<string,items>::iterator it; For (It=mymap.begin (); It!=mymap.end (); it++) {if (it->second.a >100) {i=mymap.erase (IT); -----> Correct mymap.erase (IT); ----->it failure. }}//first is key and second is value; for (it = Mymap.begin (); It!=mymap.end (); it++) {cout<<it->first<< "" <<it->second.a<< "" <<it->second.b<<en dl } system ("Pause"); Return 0; }</span>
Code Listing 2:
<span style= "FONT-SIZE:18PX;" > #include <map> #include <iterator> #include <string> #include <iostream> #include < Cstring> using namespace std; struct Itemstruct {int A; Char b[20]; itemstruct (int t,char*str) {a=t; strcpy (B,STR); } }; int main () {map<string,itemstruct>mymap; Mymap.insert (Make_pair ("A", Itemstruct (Ten, "Hanzhou")); Mymap.insert (Make_pair ("AB", Itemstruct ("Fuzhou"))); Mymap.insert (Make_pair ("abc", Itemstruct ("Zhengzhou"))); Mymap.insert (Make_pair ("ABCD", Itemstruct ("Wuhan"))); Mymap.insert (Make_pair ("ABCDE", Itemstruct ("Kunming"))); Mymap.insert (Make_pair ("ABCdef", Itemstruct ("Xiamen"))); Map<string,itemstruct>::iterator It=mymap.begin (); while (It!=mymap.end ()) {if (It->second). a>100) Mymap.erase (it++); else it++; } it=mymap.begin (); while (It!=mymap.end ()) {cout<<it->first<< "" << (It->second) .a<< "" << (It->second) .b<<endl; it++; } System ("PAUSE"); return 0; }</span>
Code Listing 3:
<span style= "FONT-SIZE:18PX;" >for (map<string, itemstruct>::iterator i = mymap.begin (); I! = mymap.end ();) { if (i->second.a > ) i = mymap.erase (i); else ++i;} </span>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Instructions for using map containers in C + +