Map is an associative container of STL, it provides one-to-one (where the first one can be called a keyword, each keyword can only appear once in a map, the second may be called the value of that keyword), and because of this feature it is possible that when we are dealing with one-to-one data, Provides fast access to programming. Here's the organization of the map's internal data, within the map, a red and black tree is built (a balanced binary tree in a strict sense), which has the ability to automatically sort the data, so all the data within the map is orderly, and we see the orderly benefits behind it.
The following example shows what a one-to-one data map is. For example, in a class, each student's number and his name there is a one by one mapping relationship, the model with a map can be easily described, it is obvious that the number in the int description, the name of the string description (in this article without char * to describe the string, but in the STL string to describe), The map description code is given below:
Map<int, string> mapstudent;
1. Map's constructor
Map altogether provides 6 constructors, which relate to the memory allocator of these things, skip the table, and in the following we'll touch on some of the map's construction methods, and here's what we usually construct a map with the following method:
Map<int, string> mapstudent;
2. Insertion of data
After the map container is constructed, we can insert data into it. Here are three ways to insert data:
The first: insert pair data with the Insert function, the following examples (although the following code is written, should be able to compile in VC and GCC, we can run under what effect, under VC Please add this statement, shielding 4786 warning #pragma Warning (disable:4786))
#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int Main ()
{
map<int, string> mapstudent;
Mapstudent.insert (Pair<int, string> (1, "Student_one"));
Mapstudent.insert (Pair<int, string> (2, "student_two"));
Mapstudent.insert (Pair<int, string> (3, "Student_three"));
Map<int, String>::iterator iter;
for (iter = Mapstudent.begin (); Iter!= mapstudent.end (); iter++)
{
cout<<iter->first<< " "<<iter->second<<end;
}
}
Second: insert value_type Data with the Insert function, as illustrated below
#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int Main ()
{
map<int, string> mapstudent;
Mapstudent.insert (Map<int, String>::value_type (1, "Student_one"));
Mapstudent.insert (Map<int, String>::value_type (2, "student_two"));
Mapstudent.insert (Map<int, String>::value_type (3, "Student_three"));
Map<int, String>::iterator iter;
for (iter = Mapstudent.begin (); Iter!= mapstudent.end (); iter++)
{
cout<<iter->first<< " "<<iter->second<<end;
}
}
The third type: insert data in an array, as illustrated below
#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int Main ()
{
map<int, string> mapstudent;
MAPSTUDENT[1] = "Student_one";
MAPSTUDENT[2] = "Student_two";
MAPSTUDENT[3] = "Student_three";
Map<int, String>::iterator iter;
for (iter = Mapstudent.begin (); Iter!= mapstudent.end (); iter++)
{
cout<<iter->first<< " "<<iter->second<<end;
}
}
The above three uses, although the data can be inserted, but they are different, of course, the first and second in the effect is complete the same, insert the data with the Insert function, the insertion of the data involved in the concept of the uniqueness of the collection, that is, when the map has this keyword, Insert operation is not inserted data, but the array is different, it can overwrite the previous keyword corresponding to the value, with the program description
Mapstudent.insert (Map<int, String>::value_type (1, "Student_one"));
Mapstudent.insert (Map<int, String>::value_type (1, "student_two"));
After these two statements are executed, the value of the 1 keyword in the map is "Student_one" and the second statement does not take effect, then this involves how we know whether the INSERT statement was inserted successfully, and can use pair to obtain the success of the insert, as follows
Pair<map<int, String>::iterator, bool> Insert_pair;
Insert_pair = Mapstudent.insert (Map<int, String>::value_type (1, "Student_one"));
We know whether the insert succeeds through the second variable of Pair, and its first variable returns a map iterator, or False if the insert succeeds Insert_pair.second should be true.
The completion code is shown below to demonstrate the success or failure of the insert.
#include <map> #include <string> #include <iostream> Using Namespa
CE std;
Int Main () {map<int, string> mapstudent;
Pair<map<int, String>::iterator, bool> Insert_pair;
Insert_pair = Mapstudent.insert (Pair<int, string> (1, "Student_one"));
If (Insert_pair.second = = True) {cout<< Insert successfully <<endl;
Else {cout<< "Insert failure" <<endl;
} Insert_pair = Mapstudent.insert (Pair<int, string> (1, "student_two"));
If (Insert_pair.second = = True) {cout<< Insert successfully <<endl;
Else {cout<< "Insert failure" <<endl;
} map<int, String>::iterator iter; for (iter = Mapstudent.begin (); Iter!= mapstudent.end (); iter++) {cout<<iter->first<< "" <<ite
r->second<<end; }
}
You can use the following program to see the effect of inserting an array into the data overlay
#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int Main ()
{
map<int, string> mapstudent;
MAPSTUDENT[1] = "Student_one";
MAPSTUDENT[1] = "Student_two";
MAPSTUDENT[2] = "Student_three";
Map<int, String>::iterator iter;
for (iter = Mapstudent.begin (); Iter!= mapstudent.end (); iter++)
{
cout<<iter->first<< " "<<iter->second<<end;
}
}
3. Size of Map
Inserting data into the map, how do we know how much data is currently plugged in, we can use the size function as follows:
Int nsize = Mapstudent.size ();
4. Traversal of data
There are also three ways to traverse a map
The first: The application of Forward iterators, the above example program is everywhere, skip the table
The second: the use of reverse-phase iterators, the following examples illustrate, to realize the effect, please run the program from the hands
#include <string>
#include <iostream>
Using namespace std;
Int Main ()
{
map<int, string> mapstudent;
Mapstudent.insert (Pair<int, string> (1, "Student_one"));
Mapstudent.insert (Pair<int, string> (2, "student_two"));
Mapstudent.insert (Pair<int, string> (3, "Student_three"));
Map<int, String>::reverse_iterator iter;
for (iter = Mapstudent.rbegin (); Iter!= mapstudent.rend (); iter++)
{
cout<<iter->first<< " "<<iter->second<<end;
}
}
The third type: Using an array method, the program description is as follows
#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int Main ()
{
map<int, string> mapstudent;
Mapstudent.insert (Pair<int, string> (1, "Student_one"));
Mapstudent.insert (Pair<int, string> (2, "student_two"));
Mapstudent.insert (Pair<int, string> (3, "Student_three"));
int nsize = mapstudent.size ()
///Here is wrong, should be for (int nindex = 1; nindex <= nsize; nindex++)
//by rainfish
fo R (int nindex = 0; nindex < nsize; nindex++)
{
cout<<mapstudent[nindex]<<end;
}
}
5. Data lookup (including determining whether this keyword appears in the map)
Here we will realize that map guarantees orderly benefits when data is inserted.
There are a number of ways to determine whether a data (keyword) appears in the map, although the title is a lookup of the data, which is interspersed with a large number of basic map usages.
Here are three ways to find the data
First Type: use the Count function to determine whether the keyword appears, its disadvantage is that the location of the data can not be located, because of map characteristics, one-to-one mapping relationship, it determines the Count function return value of only two, or 0, or 1, the situation, of course, is to return 1
The second type: using the Find function to locate the location of the data, an iterator that returns the iterator that returns the location of the data when it appears, and if there is no data to look for in the map, the iterator returned is equal to the iterator returned by the End function, and the program description
#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int Main ()
{
map<int, string> mapstudent;
Mapstudent.insert (Pair<int, string> (1, "Student_one"));
Mapstudent.insert (Pair<int, string> (2, "student_two"));
Mapstudent.insert (Pair<int, string> (3, "Student_three"));
Map<int, String>::iterator iter;
iter = Mapstudent.find (1);
if (ITER!= mapstudent.end ())
{
cout<< "find, the value is" <<iter->second<<endl;
}
Else
{
cout<< ' do don't find ' <<endl;
}
}
The Third: This method is used to determine whether the data appear, is a bit stupid, but I intend to explain here
Lower_bound function Use, which returns the lower bound (an iterator) to find the keyword.
Upper_bound function Use, which returns the upper bound (an iterator) to find the keyword.
For example, if the 1,2,3,4 is already inserted in the map, if Lower_bound (2), the returned 2, and Upper-bound (2), the return is 3
The Equal_range function returns a pair,pair inside the first variable is the iterator that Lower_bound returns, pair the second iterator is the iterator that the Upper_bound returns, if the two iterators are equal, This keyword is not present in the map, and the program description
#include <map> #include <string> #include <iostream> Using namespace std;
Int Main () {map<int, string> mapstudent;
MAPSTUDENT[1] = "Student_one";
MAPSTUDENT[3] = "Student_three";
MAPSTUDENT[5] = "student_five";
Map<int, String>::iterator iter;
iter = Mapstudent.lower_bound (2);
{//Returns an iterator for the lower 3 cout<<iter->second<<endl;
iter = Mapstudent.lower_bound (3);
{//Returns an iterator for the lower 3 cout<<iter->second<<endl;
iter = Mapstudent.upper_bound (2);
{//Returns an iterator with an upper bound of 3 cout<<iter->second<<endl;
iter = Mapstudent.upper_bound (3);
{//Returns an iterator with an upper bound of 5 cout<<iter->second<<endl;
Pair<map<int, String>::iterator, Map<int, string>::iterator> Mappair;
Mappair = Mapstudent.equal_range (2);
if (Mappair.first = = Mappair.second) {cout<< "Don't find" <<endl;
Else {cout<< "find" <<endl;} MaPpair = Mapstudent.equal_range (3);
if (Mappair.first = = Mappair.second) {cout<< "Don't find" <<endl;
Else {cout<< "find" <<endl;} }
6. The data is emptied and sentenced to empty
Emptying the data in the map can use the clear () function to determine if there is data in the map that can be used with the empty () function, and it returns true to indicate that it is an empty map
7. Deletion of data
Here you use the Erase function, which has three overloaded functions, which are described in detail in the example below.
#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int Main ()
{
map<int, string> mapstudent;
Mapstudent.insert (Pair<int, string> (1, "Student_one"));
Mapstudent.insert (Pair<int, string> (2, "student_two"));
Mapstudent.insert (Pair<int, string> (3, "Student_three"));
If you want to demonstrate the output effect, please select one of the following, you see the effect will be better
//If you want to remove 1, use iterators to remove
map<int, String>::iterator iter;
iter = Mapstudent.find (1);
Mapstudent.erase (ITER);
If you want to delete 1, delete
Int n = mapstudent.erase (1) with the keyword, or return 1 if deleted, or 0
//With an iterator, delete//a
piece of code to empty
the entire map Mapstudent.earse (Mapstudent.begin (), Mapstudent.end ());
The deletion should note that, is also the characteristics of STL, delete interval is a front closed after the set
//from a plus traversal code, print output bar
}
8. Some other functional usage
Here are functions such as swap,key_comp,value_comp,get_allocator, feel that these functions in programming is not a lot, slightly not table, interested in words can be a research
9. Sorting
Here is a little more advanced usage, sorting problem, STL default is to use less than to sort, the above code in the sort is not any problem, because the above keyword is int type, it itself supports less than operation, in some special cases, such as the keyword is a structure, The problem involves sorting, because it does not have less than the operation, insert and other functions in the compile time, the following two methods to solve this problem
First: Less than the number of overloads, program examples
#include <map>
#include <string>
Using namespace std;
Typedef struct Tagstudentinfo
{
Int NID;
String strName;
} Studentinfo, *pstudentinfo; Student Information
int main ()
{
int nsize;
Mapping scores map<studentinfo with student information
, int>mapstudent;
Map<studentinfo, Int>::iterator iter;
Studentinfo Studentinfo;
Studentinfo.nid = 1;
Studentinfo.strname = "Student_one";
Mapstudent.insert (Pair<studentinfo, int> (Studentinfo,));
Studentinfo.nid = 2;
Studentinfo.strname = "Student_two";
Mapstudent.insert (Pair<studentinfo, int> (Studentinfo));
For (Iter=mapstudent.begin (); Iter!=mapstudent.end (); iter++)
cout<<iter->first.nid<<endl< <iter->first.strName<<endl<<iter->second<<endl;
}
The above program is not compiled through, as long as the overload is less than, OK, as follows:
Typedef struct Tagstudentinfo
{
Int NID;
String StrName;
Bool operator < (tagstudentinfo const& _a) const
{
//This function specifies the sort policy, sorted by Nid, and if Nid is equal, sort by strname if
( NID < _a.nid) return true;
If (NID = = _a.nid) return Strname.compare (_a.strname) < 0;
return false;
}
} Studentinfo, *pstudentinfo; Student Information
Second: The application of the imitation function, this time the structure is not directly less than the number of overloads, program description
#include <map>
#include <string>
Using namespace std;
Typedef struct Tagstudentinfo
{
Int NID;
String strName;
} Studentinfo, *pstudentinfo; Student Information
Classs sort
{public
:
Bool operator () (Studentinfo const &_a, Studentinfo const &_B ) Const
{
If (_a.nid < _b.nid) return true;
If (_a.nid = = _b.nid) return _a.strname.compare (_b.strname) < 0;
return false;
}
;
int main ()
{
//mapping fractions with student information
map<studentinfo, int, sort>mapstudent;
Studentinfo Studentinfo;
Studentinfo.nid = 1;
Studentinfo.strname = "Student_one";
Mapstudent.insert (Pair<studentinfo, int> (Studentinfo,));
Studentinfo.nid = 2;
Studentinfo.strname = "Student_two";
Mapstudent.insert (Pair<studentinfo, int> (Studentinfo));
10. In addition
Because the STL is a unified whole, many of the uses of map are combined with other things in the STL, such as in the sort, where the default is less than, that is less<> if you want to sort from big to small, there are a lot of things here that can't be explained here.
It is also to be explained that map because of its internal order, guaranteed by the red-black tree, so many functions performed by the time complexity is log2n, if the map function can be implemented, and STL algorithm can also complete the function, it is recommended to use a map with the function, more efficient.
Next, the map in the space of the characteristics, otherwise, it is estimated that you will sometimes be more depressed, because the map of each data corresponding to a red-black tree node, this node does not save your data, is occupied 16 bytes, a parent pointer, the child pointer, There is also an enumeration value (labeled Red and black, equal to the balance factor in the balanced binary tree), and I think you should know that these places are very memory-like.
The above is a small series for everyone to talk about the STL in C + + in the map usage detailed content, I hope that we support cloud Habitat Community ~