STL 6: map/multimap usage

Source: Internet
Author: User
Tags map class
Document directory
MAP/multimap

Before using map/multimap, add the header file # include <map>. Map and multimap manage key/value as elements. They can automatically sort elements based on the sorting rules of keys. Multimap allows repeated elements, while map does not allow repeated elements.

The internal data structures of MAP and multimap are also balanced binary trees.

Map and multimap automatically sort elements based on the key of the element. to modify the key of an element, you must first Delete the element with the key and then insert the element with the new key/value.

Common functions 1. constructor and destructor

Map M: creates an empty ing and does not contain any elements.

Map M (OP): Uses OP as the sorting criterion to generate an empty map.

Map M1 (m2): Copies the elements in C2 to C1.

Map M (const value_type * First, const value_type * Last): Copies the elements between [first, last) to form a new ing.

Map M (const value_type * First, const value_type * Last, OP): Uses OP as the sorting criterion and copies the elements of [first, last) to form a new ing.

M .~ Set () destroys all elements and releases memory

Multimap mm: creates an empty ing and does not contain any elements.

Multimap mm (OP): Uses OP as the sorting criterion to generate an empty multimap

Multimap M1 (m2): Copies the elements in m2 to M1.

Multimap M (const value_type * First, const value_type * Last): Copies the elements between [first, last) to form a new ing.

Multimap M (const value_type * First, const value_type * Last, OP): Uses OP as the sorting criterion and copies the elements between [first, last) to form a new ing.

M .~ Multimap () destroys all elements and releases memory

#include "stdafx.h"#include <iostream>#include <map>using namespace std;bool fncomp (char lhs, char rhs) {return lhs<rhs;}struct classcomp {bool operator() (const char& lhs, const char& rhs) const{return lhs<rhs;}};int main (){map<char,int> first;first['a']=10;first['b']=30;first['c']=50;first['d']=70;map<char,int> second (first.begin(),first.end());map<char,int> third (second);map<char,int,classcomp> fourth;                 // class as Comparebool(*fn_pt)(char,char) = fncomp;map<char,int,bool(*)(char,char)> fifth (fn_pt); // function pointer as Comparereturn 0;}

2. Size, empty function judgment

Int size () const: returns the number of elements in the container.
Bool empty () const: determines whether the container is empty. If true is returned, the container is empty.

3. added the deletion function.

Iterator insert (const value_type & X): insert element x

Iterator insert (iterator it, const value_type & X): insert element x in it of iteration pointer
Void insert (const value_type * First, const value_type * Last): insert [first,
Last) Elements

Iterator erase (iterator it): deletes elements in the IT field of the iteration pointer.

Iterator erase (iterator first, iterator last): Delete [first,
Last) Elements

Size_type erase (const key & Key): deletes an element whose key value is equal to the key.

#include "stdafx.h"#include <iostream>#include <map>using namespace std;int main (){map<char,int> mymap;mymap.insert(pair<char,int>('a',10));mymap.insert(pair<char,int>('z',200));pair<map<char,int>::iterator,bool> ret;ret = mymap.insert(pair<char,int>('z',500));if (ret.second == false){cout<<"element 'z' already existed";cout<<"with a value of "<<ret.first->second<<'\n';}map<char,int>::iterator it = mymap.begin();mymap.insert(it,pair<char,int>('b',300));mymap.insert(it,pair<char,int>('c',400));map<char,int> anothermap;anothermap.insert(mymap.begin(),mymap.find('c'));cout<<"mymap contains :\n";for (it = mymap.begin();it!= mymap.end();it++){cout<<it->first<<"=>"<<it->second<<'\n';}cout<<"anothermap contains :\n";for (it = anothermap.begin();it!= anothermap.end();it++){cout<<it->first<<"=>"<<it->second<<'\n';}return 0;}

The above code runs as follows:

#include "stdafx.h"#include <iostream>#include <map>using namespace std;int main (){map<char,int> mymap;map<char,int>::iterator it;mymap['a'] = 10;mymap['b'] = 20;mymap['c'] = 30;mymap['d'] = 40;mymap['e'] = 50;mymap.insert(pair<char,int>('f',60));cout<<"initial mymap contains :\n";for (it = mymap.begin();it!= mymap.end();it++){cout<<it->first<<"=>"<<it->second<<'\n';}it = mymap.find('b');mymap.erase(it);mymap.erase('c');it = mymap.find('e');mymap.erase(it,mymap.end());cout<<"now mymap contains :\n";for (it = mymap.begin();it!= mymap.end();it++){cout<<it->first<<"=>"<<it->second<<'\n';}return 0;}

The above code runs as follows:


If you want to modify a ing value in MAP/multimap, you should first Insert a new ing and then delete the ing with the modification.

4. traversal Functions

Iterator begin (): returns the iterator pointer to the first element.

Iterator end (): returns the iterator pointer to the end element.

Reverse_iterator rbegin (): returns the reverse iterator pointer of the last element.

Reverse_iterator rend (): returns the iterator pointer at the previous position of the first element.

5. operation functions
Const_iterator lower_bound (const key & Key): const_iterator upper_bound (const key & Key ): iterator pointer int count (const key & Key) const whose return key value is greater than key: number of elements whose return key value is equal to key pair <const_iterator, const_iterator> pai_range (const key & Key) const: returns the iteration pointer [first, last) const_iterator find (const key & Key) const of the container whose key value is the same as the key. The query function is provided, iterator pointer void swap (set & S) with the return key value equal to the key: exchanged but mapped element void swap (Multiset & S): exchanged multi- ing Element
6. Special Functions

Reference operator [] (const key & K): only in the ing map class, you can add a key-value pair to the ing as an array and reference the returned 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.