Map usage in C ++

Source: Internet
Author: User
Basic operations and usage of C ++ Map

Source: (http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html)
-Basic operations and use of C ++ map _ live _ Sina Blog

Map is a standard container of C ++. It provides a good one-to-one relationship. Creating a map in some programs can get twice the result with half the effort, summarized some basic simple and practical operations of map!
1. The most basic constructor of map;
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 to 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 common insert and add operations in map!
3. Search for elements in map:

The find () function returns an iterator pointing to an element whose key value is key. If no key is found, the iterator pointing to the end of map is returned.

Map <int, string >:: iterator l_it ;;
Rochelle it = maplive. Find (112 );
If (l_it = maplive. End ())
Cout <"we do not find 112" <Endl;
Else cout <"wo find 112" <Endl;
4. Deletion of elements in map:
If 112 is deleted;
Map <int, string >:: iterator l_it ;;
Rochelle it = maplive. Find (112 );
If (l_it = maplive. End ())
Cout <"we do not find 112" <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 an exchange between two containers;
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;
}

6. Map sort problems:
The elements in map are automatically sorted in ascending order of keys, so the sort function cannot be used 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
Press any key to continue...

7. Basic Map operation functions:
C ++ maps is an associated container that contains "key/value" Pairs
Begin () returns the iterator pointing to the map Header
Clear () delete all elements
Count () returns the number of times a specified element appears.
Empty () returns true if map is empty.
End () returns the iterator pointing to the end of the map.
Performance_range () returns the iterator pair of a special entry
Erase () deletes an element.
Find () to find an element
Get_allocator () returns the map Configurator
Insert () insert element
Key_comp () returns the function of comparing element keys.
Lower_bound () Return 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 pointing to the end of the map.
Rend () returns a reverse iterator pointing to the map header.
Size () returns the number of elements in the map.
Swap () exchange two maps
Upper_bound () Return key value> the first position of the given element
Value_comp () returns the function of comparing element values.

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.