C + + Learning-Map Understanding

Source: Internet
Author: User

    • Map
    • Map iterator
      • Iterator invalidation
    • Map initialization
    • Map Insert Element

Map

The feature of map is that all key values are automatically sorted. All elements are pair types , with both an entity value (value) and a key value (key). The first element of the pair is treated as a key value and the second as a real value.

The same set,map does not allow two elements to have the same key value. Because of the automatic arrangement of key values, there must be a strict weak ordering defined on the key type. The so-called strict weak ordering can be understood as less than the relationship on the key value type.

So if there are no less than two key values, the container will be treated as the same key.

Map belongs to associative container

The bottom of the map is made with red and black trees.

Map iterator

The map iterator cannot change the key value of the element, but it can change the real value. Since the key values are automatically arranged, changing the key values will affect the organization of the map, so map iterator is not a constant iterator, nor is it a mutable iterator.

A map iterator will result in the pair type object being dereferenced.

Iterator invalidation

Map has some of the same properties as list: When the client adds an element to it (insert) and deletes it, it is still valid before and after the operation, except for the element that was deleted, of course.

Let's take a look at the source code of the map of the part I intercepted.

//map source code from Map.h //Modifiers:      STD::p Air<iterator,BOOL> Insert (Constvalue_type& __x) {typedef TypeName_base::iterator _base_iterator;STD::p Air<_base_iterator,BOOL> __res = _base::insert (__x);return STD::p Air<iterator,BOOL> (Iterator (__res.first, This), __res.second); } iterator Insert (iterator __position,Constvalue_type& __x) {__glibcxx_check_insert (__position);returnIterator (_base::insert (__position.base (), __x), This); }Template<TypeName_inputiterator>voidInsert (_inputiterator __first, _inputiterator __last) {__glibcxx_check_valid_range (__first, __last);    _base::insert (__first, __last); }voidErase (iterator __position) {__glibcxx_check_erase (__position);    __position._m_invalidate ();      _base::erase (__position.base ()); } Size_type Erase (Constkey_type& __x) {iterator __victim = find (__x);if(__victim = = End ())return 0;Else{__victim._m_invalidate (); _base::erase (__victim.base ());return 1; }      }voidErase (iterator __first, iterator __last) {//_glibcxx_resolve_lib_defects    //151. Can ' t currently clear () empty container__glibcxx_check_erase_range (__first, __last); while(__first! = __last) This->erase (__first++); }

The above part of the code does not need to look difficult, in fact, some function declarations and overloads.

We can see that when inserting an element using INSERT, in addition to initializing with the start pointer and end pointer method, the other will return the new required element, and when the iterator is inserted, the position of the new element's iterator will be returned.

So when we are using:

iter = map.insert(iter, value);

In this way, there will be no iterator invalidation.

But erase is not, because only the erase of the sequence container returns the iterator of the next element after the deletion. And map this kind of association container, from the source code can see the return value is void.

So when we delete the operation:

map.erase(iter++);

Just like that.

Map initialization
    • map<k, v> m;Creates an empty map object of M.
    • map<k, v> m(m2);Create a copy of M2, k,v must be of the same type
    • map<k, v> m(b, e);Creates a copy of all elements within the range of iterator B and e tags. This element type must be able to be converted topair<const k, v>
Map Insert Element

Define a map: map <string, int> word;
1. Relatively simple, direct subscript: word["alps"] = 1;
2. Use Insert: word.insert(make_pair("alps", 1)); yes.
3. or typedef:typedef map<string, int>::value_type valType; word.insert(valType("alps", 1));

Roughly as above.

C + + Learning-Map Understanding

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.