C + + map usage inductive 2

Source: Internet
Author: User

"Respecting the original works: http://blog.csdn.net/zcf1002797280/article/details/7847819"


map is a standard C + + container, it provides a good one-to-one relationship, in some programs to build a map can play a multiplier effect, summed up some map basic simple and practical operation!
1. Map constructor;
map<string, int >mapstring; map<int, string >mapint;
map<sring, char>mapstring; map< char,string>mapchar;
Map<char,int>mapchar; map<int, char >mapint;
2. Map add data;
map<int,string> maplive;
1.maplive.insert (pair<int,string> (102, "aclive"));
2.maplive.insert (Map<int,string>::value_type (321, "Hai"));
3, maplive[112]= "April";//map the simplest and most common inserts to add!
find the elements in the 3,map:
The find () function returns an iterator that points to an element with a key value, and returns an iterator to the tail of the map if it is not found.
map<int, String >::iterator l_it;;
L_it=maplive.find (a);
if (L_it==maplive.end ())
cout<< "We do not find" <<endl;
else cout<< "wo find" <<endl;
deletion of elements in 4,map:
if you delete the;
map<int, String >::iterator l_it;;
L_it=maplive.find (a);
if (L_it==maplive.end ())
cout<< "We do not find" <<endl;
Else Maplive.erase (l_it);//delete ;
usage of swap in 5,map:
swap in map is not an element exchange in a container, but a two container exchange;
For Example:
#include <map>
#include <iostream>
using namespace std;
int main ()
{
map <int, int> M1, M2, M3;
map <int, int>::iterator M1_iter;
M1.insert (Pair <int, int> (1, ten));
M1.insert (Pair <int, int> (2));
M1.insert (Pair <int, int> (3,));
M2.insert (Pair <int, int> (Ten));
M2.insert (Pair <int, int> ());
M3.insert (Pair <int, int> (+));
cout << "The original map M1 is:";
For (M1_iter = M1.begin (); M1_iter! = M1.end (); m1_iter++)
cout << "" << m1_iter->second;
cout << "." << Endl;
//This is the member function version of Swap
//M2 is said to be the argument map; M1 the target map
M1.swap (m2);
cout << "after swapping with M2, map M1 is:";
For (M1_iter = M1.begin (); M1_iter! = M1.end (); m1_iter++)
cout << "<< m1_iter, second;
cout << "." << Endl;
cout << "after swapping with M2, map m2 is:";
For (M1_iter = M2.begin (); M1_iter! = M2.end (); m1_iter++)
cout << "<< m1_iter, second;
cout << "." << Endl;
//This is the specialized template version of Swap
swap (M1, M3);
cout << "after swapping with M3, map M1 is:";
For (M1_iter = M1.begin (); M1_iter! = M1.end (); m1_iter++)
cout << "<< m1_iter, second;
cout << "." << Endl;
}
sort problem with 6.map:
the elements in map are automatically sorted by key in ascending order, so you cannot use the sort function for map:
For Example:
#include <map>
#include <iostream>
using namespace std;
int main ()
{
map <int, int> M1;
map <int, int>::iterator M1_iter;
M1.insert (Pair <int, int> (1));
M1.insert (Pair <int, int> (4, Max));
M1.insert (Pair <int, int> (3));
M1.insert (Pair <int, int> (2,));
M1.insert (Pair <int, int> (6, Max));
M1.insert (Pair <int, int> (7,));
cout << "The original map M1 is:" <<endl;
For (M1_iter = M1.begin (); M1_iter! = M1.end (); m1_iter++)
cout << m1_iter->first<< "" <<m1_Iter->second<<endl;

}
The original map M1 is:
1
2
3
4
6
7
Please press any key to continue ...
7, the basic operation function of map:
C + + maps is an associative container that contains "keyword/value" pairs
begin () returns an iterator pointing to the map header
Clear () Delete all elements
count () returns the number of occurrences of the specified element
Empty () returns True if map is empty
End () returns an iterator pointing to the end of the map
Equal_range () returns an iterator to a special entry
Erase () delete an element
find () finds an element
Get_allocator () returns the map's configurator
Insert () inserts an element
Key_comp () returns the function that compares the element key
Lower_bound () returns the key value >= the first position of a given element
max_size () returns the maximum number of elements that can be accommodated
Rbegin () returns a reverse iterator pointing to the tail of the map
rend () returns a reverse iterator pointing to the map header
size () returns the number of elements in the map
Swap () swap two maps
Upper_bound () returns the key value > the first position of a given element
Value_comp () returns a function that compares the element value

Related Article

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.