C ++ learning and learning

Source: Internet
Author: User

C ++ learning and learning

  • Map
  • Map iterator
    • Iterator failed
  • Map Initialization
  • Map insert element

Map

Map features that all key values are automatically sorted. All elements arePair typeAnd has both the object value and key value ). The first element of pair is regarded as the key value, and the second element is regarded as the real value.

In the same set, map does not allow two elements to have the same key value. Because of the Automatic Arrangement of key values, the key type must be defined.Strictly weak sorting. The so-called strict weak sorting can be understood as the key-Value TypeLessLink.

Therefore, if there is no relationship between the two key values, the container will be treated as the same key.

Map belongs to the associated container

The bottom layer of map is implemented by the red and black trees.

Map iterator

The map iterator cannot change the key value of an element, but can change the real value. Because the key values are automatically arranged, changing the key values will affect the map organization. Therefore, map iterator is neither a constant iterator nor a mutable iterator.

The pair type object is generated when the map iterator is unreferenced.

Iterator failed

Map has the same properties as list: when the client adds or deletes elements to it, it is still valid before and after the operation, except for the deleted element.

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

//map source code from map.h // modifiers:      std::pair<iterator, bool>      insert(const value_type& __x)      {    typedef typename _Base::iterator _Base_iterator;    std::pair<_Base_iterator, bool> __res = _Base::insert(__x);    return std::pair<iterator, bool>(iterator(__res.first, this),                     __res.second);      }      iterator      insert(iterator __position, const value_type& __x)      {    __glibcxx_check_insert(__position);    return iterator(_Base::insert(__position.base(), __x), this);      }      template<typename _InputIterator>        void        insert(_InputIterator __first, _InputIterator __last)        {      __glibcxx_check_valid_range(__first, __last);      _Base::insert(__first, __last);    }      void      erase(iterator __position)      {    __glibcxx_check_erase(__position);    __position._M_invalidate();    _Base::erase(__position.base());      }      size_type      erase(const key_type& __x)      {    iterator __victim = find(__x);    if (__victim == end())      return 0;    else    {      __victim._M_invalidate();      _Base::erase(__victim.base());      return 1;    }      }      void      erase(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 Code does not need to seem very difficult, but it is actually some function declaration and heavy load.

We can see that when inserting Elements Using insert, in addition to using the start pointer and end pointer Method for initialization, others will return New required elements. when inserting elements using the iterator, returns the iterator position of the new element.

So when we use it:

iter = map.insert(iter, value);

In this way, the iterator will not become invalid.

But erase won't work, because only the returned result of the sequence container's erase is the iterator of the next element after deletion, and the associated container map can see from the source code that the returned value is void.

Therefore, when deleting an object:

map.erase(iter++);

In this way.

Map Initialization
  • map<k, v> m;Create an empty map object of m.
  • map<k, v> m(m2);Create a copy of m2, k and v must be of the same type
  • map<k, v> m(b, e);Create copies of all elements in the range of iterator B and e. This element type must be convertedpair<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));You can.
3. Or typedef:typedef map<string, int>::value_type valType; word.insert(valType("alps", 1));

As shown in the preceding figure.

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.