C + + map uses

Source: Internet
Author: User

1. Map constructor
Map<int,string> Maphai;
Map<char,int> Maphai;
Map<string,char> mapstring;
Map<string,int> mapstring;
map<int,char>mapint;
map<char,string>mapchar;
2. Insertion of data
After constructing the map container, we can insert data into it. Here are three ways to insert data:
First: Insert the pair data with the 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<<endl;
}
return 0;
}
The second type: Insert the Value_type data with the Insert function, the following examples illustrate
#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 the data in an array way, 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 kinds of usage, although can achieve the data insert, but they are different, of course, the first and the second in the effect is exactly the same, with the Insert function inserted data, the insertion of the data involves 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 value corresponding to the keyword, using the program description
Mapstudent.insert (Map<int,string>::value_type (1, "Student_one"));
Mapstudent.insert (Map<int,string>::value_type (1, "student_two"));
Above these two statements executed, the map in 1 the value of this keyword is "Student_one", the second statement does not take effect, then this relates to how we know the INSERT statement is inserted into the success of the problem, you can use the pair to get the insertion success, the program is 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 by the second variable of the Pair, and its first variable returns a map iterator that Insert_pair.second should be true if the insert succeeds, otherwise false.
The completion code is given below to demonstrate the success of the insert.
#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 procedure to see the effect of inserting an array on 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
When we insert data into a map, how do we know how much data is currently inserted, and we can use the size function as follows:
Int nSize = Mapstudent.size ();
4. Traversal of data
There are three ways to traverse a map.
The first is to apply the forward iterator, which is everywhere in the above example program, skip the table
The second kind: Apply the inverse iterator, the following example shows, to realize the effect, please run the program from a
#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 type: Using the array method, 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 ()
Error here, should be for (int nIndex = 1; nIndex <= nSize; nindex++)
by Rainfish
for (int nIndex = 0; NIndex < nSize; nindex++)
{
cout<<mapstudent[nindex]<<end;
}
}
5. Data lookup (including determining if the keyword appears in the map)
Here we will realize that map ensures an orderly benefit when data is inserted.
To determine whether a data (keyword) appears in the map of the method is more, where the title is a data lookup, here will be interspersed with a large number of map basic usage.
Here are three ways to find the data
The first: Use the Count function to determine whether the keyword appears, the disadvantage is that the location of the data can not be located, due to map characteristics, one-to-one mapping relationship, it is determined that the count function of the return value of only two, either 0, or 1, the occurrence of the situation, of course, returned 1
The second type: Use the Find function to locate the location of the data, it returns an iterator, when the data appears, it returns the iterator where the data is located, if there is no data to find in the map, it returns an iterator equal to the iterator returned by the End Function, 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 not Find" <<endl;
}
}
The third: This method is used to determine whether the data appears, it seems stupid point, but I am going to explain here
Lower_bound function usage, this function is used to return the lower bound of the keyword to find (is an iterator)
Upper_bound function usage, which is used to return the upper bound of the keyword to find (is an iterator)
For example, if 1,2,3,4 is already inserted in the map, if Lower_bound (2) returns 2, and Upper-bound (2), the return is 3.
The Equal_range function returns an iterator in which the first variable inside the Pair,pair is the Lower_bound return, and the second iterator inside the pair is the iterator that Upper_bound returns, and if the two iterators are equal, This keyword does not appear in the map, 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 to the Nether 3
cout<<iter->second<<endl;
}
iter = Mapstudent.lower_bound (3);
{
Returns an iterator to the Nether 3
cout<<iter->second<<endl;
}
iter = Mapstudent.upper_bound (2);
{
An iterator that returns an upper bound of 3
cout<<iter->second<<endl;
}
iter = Mapstudent.upper_bound (3);
{
An iterator that returns 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<< "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. Emptying of data and empty sentences
Emptying the data in the map can use the clear () function to determine if there is data in the map with the empty () function, which returns true to indicate that it is an empty map
7. Deletion of data
Here we use the Erase function, which has three overloaded functions, which are explained in detail in the examples 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, choose one of the following, and you'll see better results.
If you want to remove 1, use an iterator to remove
Map<int,string>::iterator ITER;
iter = Mapstudent.find (1);
Mapstudent.erase (ITER);
If you want to delete 1, delete it with the keyword
Int n = mapstudent.erase (1);//returns 1 if deleted, otherwise 0
Use iterators to delete slices
A little bit of code to empty the map.
Mapstudent.earse (Mapstudent.begin (), Mapstudent.end ());
Delete to be aware of, is also the characteristics of the STL, delete interval is a front closed after the set
From a plus traverse code, print out
}
8. Some other function usages
There are swap,key_comp,value_comp,get_allocator, such as functions, feel that these functions in the programming is not a lot, skip the table, interested can be self-study
9. Sorting
Here is a bit more advanced usage, sorting problems, STL default is to use less than the number to sort, the above code in the sort is no problem, because the above keyword is an int, which itself supports less than the number of operations, in some special cases, such as the keyword is a struct, Problems related to sorting, because it has no less than operation, insert and other functions at the time of compilation, the following two ways to solve the problem
The first type: 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 with student information
map<studentinfo,int>mapstudent;
Map<studentinfo,int>::iterator ITER;
Studentinfo Studentinfo;
Studentinfo.nid = 1;
Studentinfo.strname = "Student_one";
Mapstudent.insert (pair<studentinfo,int> (studentinfo,90));
Studentinfo.nid = 2;
Studentinfo.strname = "Student_two";
Mapstudent.insert (pair<studentinfo,int> (studentinfo,80));
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 overloaded less than the number, OK, as follows:
Typedef struct Tagstudentinfo
{
Int NID;
String StrName;
Bool operator < (tagstudentinfo const& _a) const
{
This function specifies the sort policy, sort 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
The second kind: the application of the pseudo function, this time there is no direct less than the number of overloaded, 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 scores with student information
map<studentinfo,int,sort>mapstudent;
Studentinfo Studentinfo;
Studentinfo.nid = 1;
Studentinfo.strname = "Student_one";
Mapstudent.insert (pair<studentinfo,int> (studentinfo,90));
Studentinfo.nid = 2;
Studentinfo.strname = "Student_two";
Mapstudent.insert (pair<studentinfo,int> (studentinfo,80));
}

C + + map uses

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.