C ++ Study Notes (2) Lenovo container

Source: Internet
Author: User
Tags lenovo

Overview
C ++ primer defines Lenovo containers as follows:

A type that holds a collection of objects that supports efficient lookup by key
In fact, unlike sequential containers, Lenovo containers search by keyword. Its underlying implementation is not a simple sequence table or linked list, but a more complex red/black tree, this data structure has the advantages of sequential tables and linked lists, and has high insertion efficiency and query efficiency. In addition, unlike a sequence container, whether it is an insert or delete operation, it will not invalidate the previous iterator (of course, except for the iterator pointing to the deleted element), which is mainly related to the implementation of its underlying data structure.

STL supports map, set, multimap, and multiset containers. Map maintains KEY-VALUE pairs consisting of keys and values, while set stores only keys. Both map and set require that keys be unique, while multimap and multiset are versions whose keys may not be unique.

In fact, on unix and linux platforms, there is a library named isc, which is a packaging library of C language. Many people think that using these functions directly is faster than STL map. In fact, they do not differ from algorithms, but from memory fragments. If you use these functions directly, you need to create new nodes. When there are many nodes and frequent deletion and insertion, the memory fragments will exist, STL uses its own Allocator to allocate memory and manages the memory in a memory pool, which greatly reduces memory fragments and improves the overall performance of the system. A predecessor named Winter tested his system. He replaced all the previous Code directly using the isc function with map, and the program speed was basically the same. After running for a long time (such as a background Service Program), the advantages of map are shown. In addition, using map greatly reduces the difficulty of coding and increases the readability of the program. Why not?


Map


As mentioned above, the key-Value pair is actually stored in map, which is a data structure defined in the utility header file. It has two public domains, first and second. Map is a container, and all the containers can be accessed using the iterator. Note that the iterated value is pair rather than value. In addition to access through the iterator, map can also use the subscript operator, that is, the key is used as an index to obtain the value. For example:

[Cpp]
Map <string, int> my_map;
My_map ["hello"] = 3;

Note that the subscript operator is used to index the value. When the key does not exist, it inserts a new key-value pair in the map. For the value, the default constructor is used. This may cause undesirable side effects in some cases. Therefore, STL also provides us with another index method, that is, the find function, it returns the pair iterator reference associated with the given key. If the key does not exist, off-the-end is returned.
Iterator. If you only want to know whether the specified KEY has been inserted in the map, you can also use another function, count, which returns the number of pair corresponding to the specified key. For map, the returned values are only 1 and 0. This function may be more useful in multimap.
For insert operations, you can directly use the subscript operator, but it will be repeated initialization for the new value. A more effective way is to use the insert function. Unlike the subscript operator, no matter which version of insert is used, if the key already exists, the addition fails. The former can be inserted successfully and the old value is replaced with the new value.

Set

For set, it is basically used in the same way as map. Because only keys exist in its data structure, it does not support subscript operators. Set is somewhat similar to a sequence container in some directions. Except for the efficiency of operations, the biggest difference between them is that set requires the KEY to be unique, so it will not be inserted repeatedly, if you use a vector iterator to initialize the set, repeated values in the set will be ignored. For example:

[Cpp]
// Define a vector with 20 elements, holding two copies of each number from 0 to 9
Vector <int> ivec;
For (vector <int >:: size_type I = 0; I! = 10; ++ I ){
Ivec. push_back (I );
Ivec. push_back (I); // duplicate copies of each number
}
// Iset holds unique elements from ivec
Set <int> iset (ivec. begin (), ivec. end ());
Cout <ivec. size () <endl; // prints 20
Cout <iset. size () <endl; // prints 10

Multimap and multiset

Multimap and multiset are actually the versions that allow duplicate keys of map and set. Their operation methods are basically the same as those of their normal versions. Of course, because they allow duplicate keys, some operations will naturally be different. For example, for multimap, it cannot use the subscript operator like map, because it saves not a separate value.

Because the key values do not need to be unique, the insert function can always be successfully executed for multimap and multiset, and a new value is added under the corresponding key (multiset only adds the key ). It should be noted that the multimap design is not similar to the map <key, vector> structure. multimap simply allows duplicate keys. That is to say, if there are three associated values with a key, there will be three pair, but their iteration sequence is continuous due to the orderliness of the red and black trees.


Author: justaipanda

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.