Map is an associative container of STL. It provides one-to-one data processing capability (the first can be called keyword, each keyword can only appear once in the map, and the second may be called the value of the keyword). Because of this feature, it is possible to provide a fast programming channel when we process one-to-one data. Let's talk about the organization of data in map. A red-black tree (a non-strictly balanced binary tree) is built in map. This tree has the function of sorting data automatically. So all data in map are orderly. We will see the benefits of ordering later.
The following example illustrates what a one-to-one data mapping is. For example, in a class, there is a one-to-one mapping relationship between each student's school number and his name. This model can be easily described by map. It is obvious that the school number is described by int and the name is described by string.
Map
1. Constructor of map
Map provides a total of six constructors. This one involves memory allocators. Skipping the list, we will touch on some map construction methods below. Here we will say that we usually construct a map by the following methods:
Map
2. Data insertion
After constructing the map container, we can insert data into it. Here are three ways to insert data:
First: insert pair data with insert function
#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;
}
}
The second is insert function to insert value_type data.
#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 is to insert data in an array. Here's an example
#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;
}
}
Although the above three usages can be used to insert data, they are different. Of course, the first one and the second are the same in effect. The insert function is used to insert data. The concept of uniqueness of the set is involved in the insertion of data. That is, when there is a key word in map, insert operation is not able to insert data, but it is not possible to insert data in array mode. Similarly, it can override the corresponding value of the previous keyword, which is explained by the program.
mapStudent.insert(map
#include <map>
#include <string>
#include <iostream>
Using namespace 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<<" "<<iter->second<<end;
}
}
You can use the following program to see the effect of inserting arrays into data coverage
#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. Map size
How do we know how many data have been inserted into the map? We can use the size function. The usage is as follows:
Int nSize = mapStudent.size();
4. Data traversal
There are also three ways to traverse a map
The first is to use forward iterators, which are everywhere in the examples above.
The second one is the application of inverted iterator. The following example shows that in order to realize the effect, please run the program by yourself.
#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>::reverse_iterator iter;
for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)
{
Cout<<iter->first<<" "<<iter->second<<end;
}
}
The third way is to use an array. The program is described 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()
//by rainfish
for(int nIndex = 1; nIndex < nSize; nIndex++)
{
Cout<<mapStudent[nIndex]<<end;
}
}
5. Data lookup (including determining whether the keyword appears in the map)
Here we will see the benefits of keeping map in order when inserting data.
There are many ways to determine whether a data (keyword) appears in a map. Although the title here is data search, there will be a lot of basic map usage interspersed here.
Three methods of data search are given here.
The first is to use the count function to determine whether the keyword appears or not. The disadvantage is that it can not locate the location of the data. Because of the characteristics of map and the one-to-one mapping relationship, it determines that the return value of the count function is only two, either 0 or 1.
The second is to use find function to locate the location of data occurrence. It returns an iterator. When data appears, it returns an iterator of the location of data. If there is no data to be looked up in map, it returns an iterator equal to the iterator returned by end function.
#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 not Find"<<endl;
}
}
Third: It's a bit clumsy to use this method to determine whether data appears or not, but I'm going to explain it here.
Lower_bound function, which returns the lower bound of the keyword to be looked up (an iterator)
Usage of the Upper_bound function, which returns the upper bound of the keyword to be looked up (an iterator)
For example, if 1, 2, 3, 4 has been inserted into the map, 2 is returned if lower_bound(2), and 3 is returned if upper-bound (2).
The Equal_range function returns a pair. The first variable in pair is the iterator returned by Lower_bound, and the second iterator in pair is the iterator returned by Upper_bound.
#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 with lower bound 3
Cout<<iter->second<<endl;
}
iter = mapStudent.lower_bound(3);
{
//Returns an iterator with lower bound 3
Cout<<iter->second<<endl;
}
iter = mapStudent.upper_bound(2);
{
//Returns an iterator for upper bound 3
Cout<<iter->second<<endl;
}
iter = mapStudent.upper_bound(3);
{
//Returns an iterator for upper bound 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<<"Do not Find"<<endl;
}
Else
{
Cout<<"Find"<<endl;
}
mapPair = mapStudent.equal_range(3);
if(mapPair.first == mapPair.second)
{
cout<<"Do not Find"<<endl;
}
Else
{
Cout<<"Find"<<endl;
}
}
6. Data Emptying and Emptying
Clearing up the data in the map can use the clear() function to determine whether there is data in the map can use the empty() function, and returning true means that it is empty map.
7. Data deletion
The erase function, which has three overloaded functions, is used here. Here's how to use them in detail in an example
#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 choose one of the following. You will see better results.
//If you want to delete 1, use iterator to delete
map<int, string>::iterator iter;
iter = mapStudent.find(1);
mapStudent.erase(iter);
//If you want to delete 1, delete it with keywords
Int n = mapStudent.erase(1);//If deleted, 1 will be returned, otherwise 0 will be returned.
//With iterator, delete in pieces
//Clear the whole map with one code
mapStudent.earse(mapStudent.begin(), mapStudent.end());
//It is also a feature of STL to delete fragments. The deletion interval is a set of front-closed and back-opened fragments.
//Add traversal code and print out
}
8. Use of other functions
Here are swap,key_comp,value_comp,get_allocator and other functions. I feel that these functions are not used very much in programming. If you are interested, you can study them by yourself.