Basic operation and use of C + + map

Source: Internet
Author: User

Basic operation and use of C + + map

Source: (http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html)-basic operation and use of C + + map _live_ Sina Blog

Map is a standard C + + container, she provides a very 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 the most basic constructor function;
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 (112);
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 112;
Map<int, string >::iterator l_it;;
L_it=maplive.find (112);
if (L_it==maplive.end ())
cout<< "We do not find" <<endl;
else Maplive.erase (L_IT); Delete 112;
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, 10));
M1.insert (Pair <int, int> (2, 20));
M1.insert (Pair <int, int> (3, 30));
M2.insert (Pair <int, int> (10, 100));
M2.insert (Pair <int, int> (20, 200));
M3.insert (Pair <int, int> (30, 300));

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, 20));
M1.insert (Pair <int, int> (4, 40));
M1.insert (Pair <int, int> (3, 60));
M1.insert (Pair <int, int> (2, 50));
M1.insert (Pair <int, int> (6, 40));
M1.insert (Pair <int, int> (7, 30));

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 20
2 50
3 60
4 40
6 40
7 30
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

Basic operation and use of C + + map

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.