Basic application of Map of C++stl

Source: Internet
Author: User
Tags new set

The basic associative containers in STL have map and set, they are all based on red and black trees, and have very high efficiency of finding and deleting.

Considerations for Using Map:

1. The key value of the associated container is not allowed to be modified, so never attempt to modify the key value of the associative container.

2, when inserting data, if you are using INSERT, and the newly inserted key value already exists in the original map, then simply inserting is not successful, does not affect the original mapping, if you use [] to insert the operation, and the newly inserted key value already exists in the original map, The real value in the original map is then changed to the real value to be inserted.

3, insert insert operation will use the pair structure, pair structure in the utility header file

4, when looking for data, if you use Find, and to find the key value does not exist, it will not affect the original collection, if you use [] lookup, and the value to find does not exist, will establish a new real value is empty, the key value is to find the element into the original map.

5. Note that the Find return value is not an integer, but an iterator that successfully returns an iterator pointing to the element being looked up, and the iterator that failed returns to the end

6, the return value of erase is an integer, returns the number of successful deleted elements, that is, successfully returned 1, failed to return 0

7, map-specific two operations:

Upper_bound lookup, which is also an iterator, if a key value exists, the iterator points to the element, and if the key value exists, the iterator points to the element with the first key value larger than the parameter
Lower_bound lookup, which returns an iterator, if a key value exists, the iterator points to the element, and if the key value exists, the iterator points to the element with the first key value smaller than the argument

1#include <iostream>2#include <map>3#include <utility>//pair's header file4#include <string>5 using namespacestd;6 intMain ()7 {8     //* * Insert data, especially when using INSERT, if the key value that you want to insert already exists, the insert operation is equivalent to invalid, and [] is used to insert, if there is already a key value to insert ,9     //then the original key value corresponding content will be rewrittenTen     //Insert version 1:pair (ITERATOR,BOOL) Insert (const pair<key_type,value_type> &value); One     //using pair to insert, the return value is a pair pair, but the content of two pairs is different, the first pair to insert is the key value, the second is a real value, A     //The return value of the pair, the first of which is a map<key_type,value_type> iterator that represents the position of the inserted data in the container, the second is the bool type, the insert successfully returns 1, otherwise returns 0; -map<int,string>Hash; -typedef pair<int,string>value; thetypedef map<int,string>:: iterator it; -typedef pair<it,BOOL>result; -Result R1=hash.insert (map<int,string>::value_type (1,"ABC")); -     //Note that this insertion is not valid because the key value 3 was previously present +Result R2=hash.insert (map<int,string>::value_type (1,"VVV")); -cout<<r1.first->first<<" "<<r1.first->second<<" "<<r1.second<<Endl; +cout<<r2.first->first<<" "<<r2.first->second<<" "<<r2.second<<Endl; AResult R3=hash.insert (Value (2,"AD"));//pair atcout<<r3.first->first<<" "<<r3.first->second<<" "<<r3.second<<Endl; -     //Insert version 2 iterator insert (iterator pos,const pair<key_type,value_type> &value) returns only the position of the inserted element in the iterator -map<int,string>:: iterator iter; -Iter=hash.insert (Hash.begin (), Value (3,"vs")); -cout<<iter->first<<" "<<iter->second<<Endl; -     //Note that this insertion is not valid because the key value 3 was previously present inIter=hash.insert (Hash.begin (), Value (3,"Vector")); -cout<<iter->first<<" "<<iter->second<<Endl; to     //Insert version 3 void Insert (Input_iterator start,input_iterator end) +     //Note that the interval of the double iterator is closed before it's opened . -map<int,string>ha; the Ha.insert (Hash.begin (), Hash.end ()); *Iter=Ha.begin (); $      for(; Iter!=ha.end (); iter++)Panax Notoginseng     { -cout<<iter->first<<' '<<iter->second<<Endl; the     } +  A     //[] Insert the data directly, if you want to insert a key value does not exist, insert a new set of mappings, put back the value is a real value, if there is a key value to insert, then the original key value corresponding data will be broken.  themap<string,string>Map_str; +map_str["Hohai"]=" -"; -cout<<map_str["Hohai"]<<Endl; $     //Note that there is a data that already exists with the same key value, which actually will change the real value corresponding to the original key value . $map_str["Hohai"]="Hello"; -cout<<map_str["Hohai"]<<Endl; -  the      -     //Find OperationsWuyi     //[] Find, from the example above can be seen using [] will return a real value of the reference, you can use this to find, but there is a disadvantage, if the value to be found does not exist will generate a thecout<<map_str["AAA"]<<Endl; -     //The Find lookup return value is an iterator, and success returns the corresponding iterator, and the end () iterator is not successfully returned Wumap<string,string>:: iterator it_str; -It_str=map_str.find ("Hohai"); About     if(it_str!=map_str.end ()) $cout<<it_str->second<<Endl; -     Else -cout<<"The element that corresponds to this key does not exist"<<Endl; -  AIt_str=map_str.find ("hehe"); +     if(it_str!=map_str.end ()) thecout<<it_str->second<<Endl; -     Else $cout<<"The element that corresponds to this key does not exist"<<Endl; the  the     //Upper_bound Lookup, which is also an iterator, if a key value exists, the iterator points to the element, and if the key value exists, the iterator points to the element with the first key value larger than the parameter the     //Lower_bound Lookup, which returns an iterator, if a key value exists, the iterator points to the element, and if the key value exists, the iterator points to the element with the first key value smaller than the argument the     //Note that the map is stored according to the structure of the red and black trees, and when the elements are added, the order is automatically adjusted, so the first one here is the value closest to the value to look for, and the value is slightly larger or smaller than the value to be looked up . -map<int,string>Map_int; inmap_int[ -]="Hello"; themap_int[ -]="Hi"; themap_int[ -]=" How is You"; Aboutmap<int,string>:: iterator it_int; theIt_int=map_int.upper_bound ( -); thecout<<it_int->first<<" "<<it_int->second<<Endl; the  +     //traversal, if the key value is more regular you can use [] to combine loops, otherwise use an iterator -  the Bayi     //Delete Data the     //There are three versions of erase in map that can delete data specified by the key value, the data specified by the iterator, and the data for the specified interval of the iterator the     //The return value of the version deleted with the key value is the number of deletions (0 or 1), and no return value for the other two versions -Cout<<map_int.erase ( -) <<Endl; -map<int,string>::iterator it_i=Map_int.begin (); the      for(; It_i!=map_int.end (); it_i++) the     { thecout<<it_i->first<<" "<<it_i->second<<Endl; the     } -Cout<<map_int.erase ( -) <<Endl; the      for(It_i=map_int.begin (); It_i!=map_int.end (); it_i++) the     { thecout<<it_i->first<<" "<<it_i->second<<Endl;94     } the     return 0; the}

Basic application of Map of C++stl

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.