11th chapter: Associative containers

Source: Internet
Author: User

Associative containers

Associative containers support efficient keyword lookups and access.

The essential difference between an associative container and a sequential container is that the associated container stores and reads the elements by key (key), while the sequential container stores and accesses the elements in the order in which they are placed in the container.

Associative containers (Associative-container): Two main associative containers: map and set

The elements of map are organized as key-value (Key-value) Pairs: The key is used as the index of the element in the map, and the value represents the data that is stored and read. The set contains only one key and effectively supports queries about the existence of a key.

In general, if you want to efficiently store collections of different values, it is more appropriate to use the set container, whereas the map container is more suitable for situations where you need to store (and even modify) the values associated with each key. When you do some text processing, you can use set to save the words that you want to ignore. And the dictionary is a good application of map: The word itself is the key, and its explanation is the value.

Both set and map types contain elements that have different keys and do not allow the addition of a second element for the same key. If a key must correspond to more than one instance, use Multimap or multi set, both of which allow multiple elements to have the same key.

The fact that the associative container elements are ordered according to the key is an important conclusion: when iterating through the associative container, we can ensure that the order of the keystrokes is accessed by the element, regardless of where the element is stored in the container.

Map type

Map is a collection of key-value pairs. The map type is usually understood as an associative array (associative array): You can use the key as the subscript to get a value, just as the built-in array type does. The essence of the association is that the value of the element is associated with a particular key, not by the position of the element in the array.

Set type

Using the Set container is most appropriate when you want to know if a value exists.

Two exceptions include: set does not support subscript operators, and mapped_type types are not defined. In the Set container, Value_type is not the pair type, but the same type as Key_type. They refer to the type of elements stored in the set. This difference also shows that the set-stored element is simply a key, without the associated value. As with map, the keys stored by the Set container must be unique and cannot be modified.

Reference Blog

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

11th chapter: Associative containers

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.