Map uses. xml

Source: Internet
Author: User

Map? Is an associated container ,?? Provide one-to-one association ,? The Association form is :? Key---value ????? The keywords are not repeated. Multimap is similar to map, but the keywords can be repeated ?????????? That is, the key word and the corresponding value ?????????????????? Keywords play the role of index ,? Search for records in map? It is to search ?????????????????? Keyword ?? And? Value? It can be of any type ????????????????? Map? It can also be seen ?? Set of keyword ing ,? That is, duplicate keywords are not allowed in map, each ing keyword is different .??????????????????????????? Map? It is based on the red-black tree structure, and its search time is log (n )? For example :?????????????? Map <int ,? Int ??????????????? // The first is the keyword, and the second is the value corresponding to this keyword ????? A keyword only corresponds to one value ,? Is a one-to-one ing ?????????????? Map <cstring, int> ?????????????? Map <int ,? Cstring> ?????????????? Map <cstring, cstring >????????????????= ???????????????........

Header file

 

???? # Include? <Map> ?????? Using? Namespace? STD ;?? // Must be added ??

1? Insert element?

???? 1 )?? Insert function insert

 

???? Map <int, int>? Idmap ;?????? Idmap. insert (pair <int, int> ));?????? Idmap. insert (Map <int, int >:: value_type (2, 1 ));????? Using the insert function to insert data involves the concept of uniqueness of the Set in data insertion. That is, when the map contains this keyword, the insert operation cannot insert data ??? Determine whether the insertion is successful?

 

???? Map <int, int>? Idmap ;?????? Idmap. insert (pair <int, int> ));?????? Idmap. insert (Map <int, int >:: value_type (2, 1 ));???????????? Pair <Map <int, int >:: iterator, bool>? Insertpair ;???????????? Insertpair = idmap. insert? (Pair <int, int> ));???????????? If? (Insertpair. Second = true )?????? {?????????? Cout <"insert? Successfully ";??????} Else ?????????? Cout <"insert? Failure ";???? 2 ?) Array Insertion Method

 

???? Map <int, int>? Idmap ;?????? Idmap [1] = 2 ;????? The array method is different. It can overwrite the value corresponding to the previous keyword ?? However, there is a performance problem. This method assigns each inserted value to the default value and then to the displayed value. If the element is a class object, the overhead is relatively large. The insert method can be used to directly assign values to the displayed values.

2? Determine whether there is

Returns? The? Number? Of? Elements? In? A? Map? Whose? Key? Matches? A? Parameter-specified? Key .? Size_type? Count (??? Const? Key &? _ Key )? Const

1? If? The? Map? Contains? An? Element? Whose? Sort? Key? Matches? The? Parameter? Key ;? 0? If? The? Map? Does? Not? Contain? An? Element? With? A? Matching? Key.

 

???? Int? Num = idmap. Count (1 );?????? If? (Num = 0 )?????? {?????????? Cout <"? Key? 1? Does? Not? Exist ";??????} Else {?????????? Cout <"exist ";??????}??

3? Search for keywords

Iterator? Find (??? Const? Key &? _ Key); const_iterator? Find (??? Const? Key &? _ Key )? Const;

 

???? Map <int, int >:: iterator? It ;?????? It = idmap. Find (2 );?????? If? (It = idmap. End ())?????? {?????????? Cout <"can? Not? Find? 2 ";??????} Else {?????????? Int? First = it-> first ;?????????? Int? Second = it-> second ;??????}??? Extension :? Iterator? Lower_bound (??? Const? Key &? _ Key); const_iterator? Lower_bound (??? Const? Key &? _ Key )? Const; returns? An? Iterator? To? The? First? Element? In? A? Map? With? A? Key? Value? That? Is? Equal? To? Or? Greater? Than? That? Of? A? Specified? Key.

The first element equal to or greater than the key

? Iterator? Upper_bound (??? Const? Key &? _ Key); const_iterator? Upper_bound (??? Const? Key &? _ Key )? Const; returns? An? Iterator? To? The? First? Element? In? A? Map? That? With? A? Key? Having? A? Value? That? Is? Greater? Than? That? Of? A? Specified? Key.

The first element greater than the key

? Pair? <Const_iterator ,? Const_iterator>? Performance_range? (??? Const? Key &? _ Key )? Const; pair? <Iterator ,? Iterator>? Performance_range? (??? Const? Key &? _ Key );????? A? Pair? Of? Iterators? Such? That? The? First? Is? The? Lower_bound? Of? The? Key? And? The? Second? Is? The? Upper_bound? Of? The? Key .???? The pai_range function returns a pair. The first variable in pair is the iterator returned by lower_bound. The second iterator in pair is the iterator returned by upper_bound. If the two iterators are equal, this keyword is not displayed in map.

 

???? P2? =? M1.equal _ range (? 4 ?);???????????? //? If? No? Match? Is? Found? For? The? Key ,?????? //? Both? Elements? Of? The? Pair? Return? End (?)?????? If? (? (? P2.first? ==? M1.end (?)?)? &&? (? P2.second? ==? M1.end (?)?)?)????????? Cout? <? "? Map? M1? Doesn' t? Have? An? Element? "?????????????? <? "? A? Key? Less? Than? 40 ."? <? Endl ;?????? Else ????????? Cout? <? "? Element? Of? Map? M1? With? A? Key?> =? 40? Is :? "?????????????? <? P2.first? ->? First? <? "."? <? Endl ;??

4? Size, number of elements contained

Returns? The? Number? Of? Elements? In? The? Map .? Size_type? Size (?)? Const;

 

???? Int? Nsize = idmap. Size ();??

5? Traversal

Forward iterator

 

???? Map <int, int >:: iterator? It ;?????? For? (It = idmap. Begin? (); It! = Idmap. End (); It ++ )?????? {?????????? Cout <it-> first <Endl ;?????????? Cout <it-> second <Endl ;??????}??

Reverse iterator

 

???? Map <int, int >:: reverse_iterator? ITER ;?????? For? (Iter = idmap. rbegin? (); Iter! = Idmap. rend? (); ITER ++ )?????? {?????????? Cout <ITER-> first <Endl ;?????????? Cout <ITER-> second <Endl ;??????}??

6? Delete

Iterator? Erase (??? Iterator? _ Where); iterator? Erase (??? Iterator? _ First ,??? Iterator? _ Last); size_type? Erase (??? Const? Key_type &? _ Key );

 

???? // Delete the iterator ?????? Map <int, int >:: iterator? It ;?????? It = idmap. Find (1 );?????? Idmap. Erase (it );???????????? // Delete the keyword ?????? Idmap. Erase (1 );???????????? // Delete parts? Or clear ?????? Idmap. Erase (idmap. Begin? (), Idmap. End ());???????????? // Clear ?????? Idmap. Clear? ();??

7? Basic 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 occurrences of a specified element ?????? Empty ()?????????? If map is empty, true ?????? End ()???????????? Returns the iterator pointing to the end of the map ?????? Performance_range ()???? Returns the iterator pair ?????? Erase ()?????????? Delete an element ?????? Find ()??????????? Search for an element ?????? Get_allocator ()?? Returns the map configurator ?????? Insert ()????????? Insert elements ?????? Key_comp ()??????? Returns the comparison element key function ?????? 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 comparison element value function ??

Example:

 

???? Map <int, vector <int >> ?? M_dianmingmap ;??

 

???? Adddianmingmap (Int? Nxueqi, Int? Nxuehao )?????? {?????????????????????????? // Add the student to the named container ???????????????????? Map <int, vector <int> >:: iterator? It ;?????????????? It = m_dianmingmap.find (nxueqi );?????????????? If? (It = m_dianmingmap.end? ())? // First, find the keyword for this semester ID ?????????????? {??????????????????? // Does not exist in the container? Then, add ?????????????????? Vector <int> ?? Int_vec ;?????????????????? Int_vec.push_back (nxuehao );???????????????????????? M_dianmingmap [nxueqi] = int_vec ;????????????????????????????????? ?????} Else {// search? During this semester? Is there a student ID ???????????????????????? Vector <int >:: iterator? Itvec = find (IT-> second. Begin? (), IT-> second. End (), m_nxuehaoid );?????????????????? If (itvec = It-> second. End ())?????????????????? {?????????????????????? // If this student does not exist, add ?????????????????????? It-> second. push_back (nxuehao );??????????????????}??????????????}? ??????????????????? Return? True ;????????????}??

Used in this article? Calligraphy and painting novel software? Released, the content has nothing to do with the software, and the painting and calligraphy novel software? More comfortable reading, more comfortable writing, and easier Publishing.

Map uses. xml

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.