[STL] Classification of associative containers

14 Day Reading Challenge

foreword

Since this article involves a lot of content, in order to be as detailed as possible, various examples will be attached, so there will be more code samples, and for a better layout, this series will be explained separately, and in the early stage for better fit For actual combat needs, only some commonly used STL containers and basic functions will be explained for the time being.

In order to make each article have a certain degree of independence, most of the subsequent articles will extract the knowledge points involved in the previous articles, and the corresponding examples will be added in the follow-up.

The STL series of knowledge in this article refers to "C++ Language Program Involvement (5th Edition)"

STL Guide

Classification of associative containers

The keys of the associative container must be able to use "<" for comparison. For types such as int and double, C++ has built-in comparison operators, and for class types, the "<" operator needs to be overloaded

Associative containers, except for unordered containers, automatically sort keys

In the worst case, a sequential container needs n comparisons before it can be found,
while an associative container uses a "balanced binary tree" structure. In the worst case, it takes about lgn comparisons to find an element

Associative containers are divided into single associative containers and multiple associative containers

single associative container

The key value in a single associative container is unique, no duplication is allowed, 集合(set)and 映射(map)it is a single associative container

multiple associative containers

In multiple associative containers, the same key value is allowed to appear repeatedly, 多重集合(multiset)and 多重映射(multimap)this is the case

simple associative container

with the element itself as the key, 集合(set)and 多重集合(multiset)of the class

binary associative container

An element is composed of a key and some type of additional data, and the key is only a part of the element
映射(map)and 多重映射(multimap)belongs to this category

Relational tables

simple associative container binary associative container
single associative container set map
multiple associative containers multiset Multimap (multimap)

Two-tuple (pair)

The elements of a binary associative container are a combination of a key type and an additional data type. This combination can be represented by a pair. A pair is a structure template defined in the <utility> header file (as follows)

template<class T1,class T2>
struct pair{
    
    
	//成员均为public
	T1 first;
	T2 second;
	pair();//默认构造函数
	pair(const T1 &x,const T2 &y);//构造first=x,second=y的二元组
	template<class U,class V>pair(const pair<U,V>&p);//复制构造函数
}

For example,
the element type of map<int, double> is pair<int, double>
multimap<string, int> is pair<string, int>
The pair type object construction supports list initialization:

pair<string,int>str_int_pair={
    
    "first",2};

Guess you like

Origin blog.csdn.net/weixin_45720193/article/details/127579598