Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the commonly used containers in C++_STL

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces what C++_STL containers are commonly used for reference, which can be used for reference by interested friends. I hope you can learn a lot after reading this article.

1. Associate the container

The associated container is the type that stores a collection of objects and supports efficient queries through keys. Most of the behavior of the associative container is the same as that of the sequential container, the essential difference between the two is that the associative container stores and reads elements through keys, while the sequential container sequentially stores and accesses elements through their position in the container.

Each element of the associated container contains a key value (key) and a value value. When an element is inserted into the associated container, the container's internal data structure places the element in place according to its key size with a specific rule. The associative container has no head and tail, only the largest element and the smallest element. The associative container does not provide front, push_front, pop_front, back, push_back, and pop_back operations.

The standard STL association containers are divided into two categories: set (set) and map (mapping), and the derivatives multiset (multi-key set) and multimap (multi-key mapping table) of these two categories. The underlying mechanism of these containers is done in RB-tree (red-black tree).

2. Common operations between associated containers and sequential containers 1. Constructors shared by associated containers and sequential containers

Associative containers share most of the operations of sequential containers, but not all of them. Associative containers can use the following three constructors:

C c

Create an empty container named c. C is the container type name, such as vector,T is the element type, such as int or string. It applies to all containers.

C c (c2)

To create a copy of container c2, ctrac and c2 must have the same container type and hold the same type of elements. It applies to all containers.

C c (b, e)

Create c whose elements are copies of the elements in the range indicated by iterators b and e. It applies to all containers.

2. Other functions shared by associated containers and sequential containers

The associated container cannot be defined by the size of the container, because it is impossible to know what the value of the key is.

The associated container supports some relational operators for sequential containers.

The associated container supports begin, end, rbegin and rend operations.

The associated container supports swap and assignment operations, but does not provide an assign function.

The associated container supports the clear and erase functions, but the erase operation of the associated container returns the void type.

The associated container supports the size (), max_size (), empty () functions as container size operations, but does not support the resize () function.

3. Operations provided for group pair types

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

Pair p1

Create an empty pair object with two elements of type T1 and T2, initialized with values.

Pair p1 (v1, v2)

Create a pair object whose two elements are of type T1 and T2, respectively, where the first member is initialized as v1MagneSsecond member initialized to v2.

Make_pair (v1, v2)

Create a new pair object with v1 and v2 values, with elements of type v1 and v2, respectively.

P1 < p2

The less than operation between two pair objects, whose definition follows dictionary order: if p1.first

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report