Map is a standard container of C++. It provides a good one-to-one relationship. Building a map in some programs can achieve twice the result with half the effort. It summarizes 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 add data;
map<int ,string> maplive;
//方式一
maplive.insert(pair<int,string>(102,"aclive"));
//方式二
maplive.insert(map<int,string>::value_type(321,"hai"));
//方式三
maplive[112]="April"; //map中最简单最常用的插入添加!
3. Find elements in the map:
The find() function returns an iterator pointing to the element whose key is key, or an iterator pointing to the end 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 112"<<endl;
else cout<<"wo find 112"<<endl;
4. Deletion of elements in map:
if delete 112;
map<int ,string >::iterator l_it;;
l_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:
The swap in Map is not the exchange of elements in one container, but the exchange of 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. The sort problem of
map: The elements in the map are automatically sorted in ascending order by key, so the sort function cannot be used for 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, 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…
- Basic operation functions of map:
C++ Maps is an associative container, containing "keyword/value" pairs
begin() Returns an iterator pointing to the head of the map
clear() Deletes all elements
count() Returns the number of occurrences of the specified element
empty () returns true if the map is empty
end() returns an iterator to the end of the map
equal_range() returns an iterator pair for a particular entry
erase() deletes an element
find() finds an element
get_allocator() returns the map's configurator
insert () Insert element
key_comp() Returns the function to compare 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 pointing to the end of the map The iterator
rend() returns a reverse iterator pointing to the head of the map
size() returns the number of elements in the map
swap() swaps two maps
upper_bound() returns the key value > the first position of the given element
value_comp() Returns a function that compares the value of an element
Source:
Blog Park