Basic operation and use of C + + map __c++

Source: Internet
Author: User

Source: http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html


Map is a standard container for C + +, she provides a good one-to-one relationship, in some programs to build a map can play a multiplier effect, summed up some of the map basic simple and practical operations.
1. Map most basic 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"; the simplest and most commonly used insertion add in//map.
Search for elements in 3,map:

The Find () function returns an iterator that points to the key value, and returns an iterator that points 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 don't find 112" <<endl;
else cout<< "wo Find 112" <<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 don't find 112" <<endl;
else Maplive.erase (L_IT); Delete 112;
Use of swap in 5,map:
The 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 are 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++)
     &n Bsp;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++)
     &n Bsp;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;
}

The sort question for 6.map:
The elements in the map are automatically sorted by key in ascending order, so you cannot use the sort function on the 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));

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 the iterator pointing to the map head
Clear () Delete all elements
COUNT () returns the number of occurrences of the specified element
Empty () returns true if the map is empty
End () returns an iterator pointing to the bottom of the map
Equal_range () returns an iterator for a particular entry
Erase () deletes an element
Find () finds an element
Get_allocator () returns the configuration of the map
Insert () Inserts an element
Key_comp () returns a function that compares the element key
Lower_bound () returns the key value >= the first position of the given element
Max_size () returns the maximum number of elements that can be accommodated
Rbegin () returns a reverse iterator that points to the tail of the map
Rend () returns a reverse iterator pointing to the map head
Size () returns the number of elements in a map
Swap () Swap two map
Upper_bound () returns the key value > The first position of the given element
Value_comp () returns a function that compares element value

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.