Home  >  Article  >  Backend Development  >  Summary of common containers in C++_STL: associative containers and sequential containers in pairs

Summary of common containers in C++_STL: associative containers and sequential containers in pairs

php是最好的语言
php是最好的语言Original
2018-07-28 11:31:122317browse

Group pair

In this part, we first introduce the common operations of associated containers and sequential containers in a pair group pair, and then introduce the commonly used ones. Several specific associated containers.

1. Associative container

Associative container is a type of storage object collection that supports efficient query by key. Most of the behavior of associative containers is the same as that of sequential containers. The essential difference between the two is that associative containers store and read elements by key, while sequential containers store and access elements sequentially by their position in the container.
Each element of the associated container contains a key and a real value. When an element is inserted into an associative container, the container's internal data structure places the element in the appropriate location according to certain rules based on the size of its key value. Associative containers have no so-called head and tail, only the largest element and the smallest element. Associative containers do not provide front, push_front, pop_front, back, push_back, and pop_back operations.
Standard STL associative containers are divided into two categories: set (set) and map (mapping), as well as their derivatives multiset (multi-key set) and multimap (multi-key mapping table). The underlying mechanisms of these containers are all implemented in RB-tree (red-black tree).

2. Common operations of associative containers and sequential containers

1. Constructors shared by associative containers and sequential containers

Associative containers share most of the operations of sequential containers, but Not all, associative containers can use the following three constructors:

C<T> c;
  • 1

Create an empty container named c. C is the container type name, such as vector, and T is the element type, such as int or string. Works with all containers.

C<T> c(c2);
  • 1

Create a copy c of container c2; c and c2 must have the same container type and store the same type of elements. Works with all containers.

C<T> c(b, e);
  • 1

Create c whose elements are copies of the elements in the range identified by iterators b and e. Works with all containers.

2. Other functions shared by associative containers and sequential containers

  1. Associative containers cannot be defined by the size of the container, because then the value corresponding to the key cannot be known What is it.

  2. Associative containers support some relational operators of sequential containers.

  3. Associated containers support begin, end, rbegin, and rend operations.

  4. Associative containers support swap and assignment operations, but do not provide the assign function.

  5. Associated containers support clear and erase functions, but the erase operation of associated containers returns the void type.

  6. Associated containers support the size(), max_size(), and empty() functions as container size operations, but do not support the resize() function.

3. Operations provided by the pair type

The pair pair contains two data values. The specific usage is as follows:

pair<T1, T2> p1;
  • 1

Create an empty pair object whose two elements are of type T1 and T2 respectively. , initialized with value.

pair<T1, T2> p1(v1, v2);
  • 1

Create a pair object, its two elements are of type T1 and T2 respectively, where the first member is initialized to v1, The second member is initialized to v2.

make_pair(v1, v2);
  • 1

Create a new pair object with v1 and v2 values, and the types of its elements are the types of v1 and v2 respectively.

p1 < p2;
  • 1

The less than operation between two pair objects, its definition follows dictionary order: if p1.first9b0d6015b2938b67bad9d94b40e0e780 Object name ;

If you use multiple identical pair objects, you can also use typedef to simplify their declaration:

typedef  pair <string, string>  Author;
Author 对象名(初始值1,初始值2);
  • 1

  • 2

For the pair class, you can directly access its data members. Its members are all public and are named first member and second member respectively. They can be accessed using the dot operator.

Related articles:

c The use of Iterator in STL standard container

c Summary of review points z-12——STL string

Related videos:

C# Tutorial

The above is the detailed content of Summary of common containers in C++_STL: associative containers and sequential containers in pairs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn