C + + Map

Source: Internet
Author: User
Tags map class

The element data of a map mapping container is a key value and a mapping data, the relationship between the key value and the mapping data has a one by one mapping.
Map mapping container data structure is implemented using red-black tree, the element inserted into the key is not allowed to repeat, the comparison function is only the key value of the element to compare, the elements of the data can be retrieved by key values.

The map class supports an associative container. A value is mapped to a unique keyword. Essentially, this mapping is a simple name for a value. Once a value is stored in the container, it can be found by keyword. The latter can be said that map is a pair of keywords/values of the list container. The power of the map container is that you can look up values by keyword. The keywords in the map container are unique and cannot be reused. However, you can use Multimap to create a map container that uses a non-unique keyword.
Using the Map container requires a header file containing the statement "#include <map>", and the map file also contains the definition of the Multimap multi-mapping container.

Std:map<int, string> personnel;

This defines an int as an index and an associated pointer to string

Changing the entries in the map is simple because the map class has overloaded the [] operator

ENUMMAP[1] = "one";

ENUMMAP[2] = "both";

Map<string,float> m;

m["Jack"] = 98.5;
m["Bomi"] = 96.0;
m["Kate"] = 97.5

Front traversal element
Map<string,float>:: Iterator it;
for (it = M.begin (); It! = M.end (); it + +)
cout << (*it). First << ":" << (*it). Second << Endl;

M.erase (28);

Reverse traversal of elements
Map<int, char>:: Reverse_iterator RIT;
for (RIT = M.rbegin (); RIT! = M.rend (); RIT + +)
22 {
23//Input key value and mapping data
cout << (*rit). First << ":" << (*rit). Second << Endl;
25}

It = m.find (+);

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

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.