总结C++顺序容器

-- TOC --

summary

前面已经有几篇笔记,涉及到了编程中最常用的顺序存储数据结构,在C++中,他们都属于顺序容器:

container adaptor:

std::array

std::array是具有固定大小的数组容器,它不支持添加或删除元素等改变大小的操作。当定义一个array时,除了指定元素类型,还要指定容器大小。

既然有了内置的普通数组,为什么还要引入array呢?

内置的数组有很多麻烦的地方,比如无法直接做对象赋值,无法直接拷贝等等,同时内置的数组又有很多比较难理解或容易出错的地方。C++11引入array容器来代替内置数组来降低对程序员的要求。

std::array的接口:

// element access
at
operator[]
front
back
data
// iterators
begin
cbegin
rbegin
crbegin
end
cend
rend
crend
// capacity
empty
size
max_size
// operations
fill
swap
// non-member functions
to_array  // creates a std::array object from a built-in array

There is a special case for a zero-length array (N == 0). In that case, array.begin() == array.end(), which is some unique value. The effect of calling front() or back() on a zero-sized array is undefined.

std::deque

std::deque (double-ended queue) is an indexed sequence container that allows fast insertion and deletion at both its beginning and its end. In addition, insertion and deletion at either end of a deque never invalidates pointers or references to the rest of the elements. deque支持indexed access,具体就是可以at或[]。deque在两端做插入和删除都很快,并且插入删除后,不会影响对其它元数的指针或引用。

As opposed to std::vector, the elements of a deque are not stored contiguously: typical implementations use a sequence of individually allocated fixed-size arrays, with additional bookkeeping, which means indexed access to deque must perform two pointer dereferences, compared to vector's indexed access which performs only one. deque内部元素并非顺序存储,典型的实现方法为使用独立申请的大小固定的array序列,并配上bookkeeping。因此,indexed access需要做两次指针的dereference。而vector在indexed access的时候,只做一次指针dereference。deque必然需要更多的internal memory cost。

https://en.cppreference.com/w/cpp/container/deque

std::queue

The std::queue class is a container adaptor that gives the programmer the functionality of a queue - specifically, a FIFO (first-in, first-out) data structure. 这就是所谓的容器适配器。

The class template acts as a wrapper to the underlying container - only a specific set of functions is provided. The queue pushes the elements on the back of the underlying container and pops them from the front. 将地层容器包裹起来,只提供一组特别的接口,外在表现为一个FIFO队列。push为地层容器的push_back,pop为地层容器的pop_front。(deque和list满足,标准默认使用deque)

https://en.cppreference.com/w/cpp/container/queue

std::forward_list

std::forward_list is a container that supports fast insertion and removal of elements from anywhere in the container. Fast random access is not supported. It is implemented as a singly-linked list. Compared to std::list this container provides more space efficient storage when bidirectional iteration is not needed. 单向链接队列,当不需要两个方向的操作的时候,forward_list的存储效率更高。内部只有head,因此很多接口都是在head部分操作。

https://en.cppreference.com/w/cpp/container/forward_list

std::stack

The std::stack class is a container adaptor that gives the programmer the functionality of a stack - specifically, a LIFO (last-in, first-out) data structure. 这是默认包裹在deque上的LIFO容器适配器。

The class template acts as a wrapper to the underlying container - only a specific set of functions is provided. The stack pushes and pops the element from the back of the underlying container, known as the top of the stack.

https://en.cppreference.com/w/cpp/container/stack

std::priority_queue

A priority queue is a container adaptor that provides constant time lookup of the largest (by default) element, at the expense of logarithmic insertion and extraction. 优先队列,用\(O(1)\)得到(默认)最大的元素。

https://en.cppreference.com/w/cpp/container/priority_queue

本文链接:https://cs.pynote.net/sf/c/cpp/202212081/

-- EOF --

-- MORE --