The use of C++map

Source: Internet
Author: User

Category: Data 2012-11-14 21:26 13378 People read comments (0) favorite reports The most complete use of C++map

This article is a copy of the 0.0

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!

3. Find the elements in 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 >::iteratorl_it;;
L_it=maplive.find (112);//Returns a pointer
if (L_it==maplive.end ())
cout<< "We do not find112" <<endl;
elsecout<< "Wo find112" <<endl;

map<string,string>m;

if (m[112]== "")

cout<< "We do not find112" <<endl;

4. Deletion of elements in 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 find112" <<endl;
else Maplive.erase (l_it);//delete 112;

5. Usage of swap in map:
Swap in map is not an element exchange in a container, but a two container exchange;
For example:
#include <map>
#include <iostream>

UsingNamespace 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 m1is:";
for (M1_iter = M1.begin (); M1_iter! = M1.end (); m1_iter++)
cout << "" <<m1_Iter->second;
cout << "." << Endl;

This isthe member function version of Swap
M2 is said to be theargument map; M1 the target map
M1.swap (m2);

cout << "afterswapping 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, MAPM2 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 << "afterswapping with M3, map M1 is:";
for (M1_iter = M1.begin (); M1_iter! = M1.end (); m1_iter++)
cout << "" << m1_iter->second;
cout << "." << Endl;
}

6. Map's sort question:
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>

UsingNamespace 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 M1is:" <<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

7. 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

Multimap [CPP]View Plaincopyprint?
  1. Multimap allow duplicate key values to be inserted into the container
  2. //        **********************************************************
  3. * Pair contains only a pair of numeric:pair<int,char> *
  4. * Map is a collection type that always keeps the order, *
  5. Pair * Map Each member is a pair, for example:map<int,char> *
  6. * Map Insert () can take a pair object as a map parameter, for example map<p> *
  7. //        ***********************************************************
  8. #pragma warning (disable:4786)
  9. #include <map>
  10. #include <iostream>
  11. Using namespace std;
  12. int main (void)
  13. {
  14. multimap<int,char*> m;
  15. //multimap inserts can only be used with the insert () array
  16. M.insert (pair<int,char*> (1,"Apple"));
  17. M.insert (pair<int,char*> (1,"pear")); Apple and pear are likely to have the same price .
  18. M.insert (pair<int,char*> (2,"banana"));
  19. //multimap traversal can only be used in iterators to not use an array
  20. cout<<"***************************************" <<endl;
  21. multimap<int,char*>::iterator i,iend;
  22. Iend=m.end ();
  23. For (I=m.begin (); i!=iend;i++)
  24. {
  25. cout<< (*i) .second<<"The Price is"
  26. << (*i) .first<<"Yuan/jin \ n";
  27. }
  28. cout<<"***************************************" <<endl;
  29. //Inverse traversal of elements
  30. multimap<int,char*>::reverse_iterator j,jend;
  31. Jend=m.rend ();
  32. For (J=m.rbegin (); j!=jend;j++)
  33. {
  34. cout<< (*j) .second<<"The Price is"
  35. << (*j) .first<<"Yuan/jin \ n";
  36. }
  37. cout<<"***************************************" <<endl;
  38. //Element search find (), Pair<iterator,iterator>equal_range (const key_type &k) Const
  39. //As with Multiset's usage
  40. multimap<int,char*>::iterator s;
  41. S=m.find (1);  //find () just find one and go back immediately.
  42. cout<< (*s) .second<<""
  43. << (*s) .first<<endl;
  44. cout<<"The number of elements with a key value equal to 1 is:" <<m.count (1) <<endl;
  45. cout<<"***************************************" <<endl;
  46. //Remove erase (), clear ()
  47. M.erase (1);
  48. For (I=m.begin (); i!=iend;i++)
  49. {
  50. cout<< (*i) .second<<"The Price is"
  51. << (*i) .first<<"Yuan/jin \ n";
  52. }
  53. return 0;
  54. }
 

The use of C++map

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.