行业知识
常用的C++ STL
2026-11-25 07:52:34
来源:互联网转载
这里写自定义目录标题
- 常用的C++ STL
- stack
- queue
- deque
常用的C++ STL
常用到的C++ STL,方便查询。
()stack
Stack是一种容器适配器,专门设计用于LIFO (last-in first-out)操作,仅从容器的一端插入删除元素(back or top)。
成员类型(c++98)
()member type definition notes value_type The first template parameter (T) Type of the elements container_type The second template parameter (Container) Type of the underlying container size_type an unsigned integral type usually the same as size_t 成员函数
函数 说明 empty Test whether container is empty size Return size top Access next element push Insert element pop Remove top element emplace(c++11) Construct and insert element swap(c++11) Swap contents queue
Queue 是一种容器适配器,专门设计用于FIFO (first-in first-out) 操作,从容器的一端插入从另一端元素(push from back and pop from front)。
成员类型(c++98)
member type definition notes value_type The first template parameter (T) Type of the elements container_type The second template parameter (Container) Type of the underlying container size_type an unsigned integral type usually the same as size_t 成员函数
函数 说明 empty Test whether container is empty size Return size front Access next element back Access last element push Insert element pop Remove next element emplace(c++11) Construct and insert element swap(c++11) Swap contents deque
双端队列(double-ended queue),是具有动态大小的容器序列,可以在两端(前端后端)扩展或收缩。
特定的库可以以不同的形式实现Deque,通常是动态数组。但在任何一种实现,他们都允许通过随机访问迭代器直接访问独立元素,并自动按需处理容量的扩展和收缩。
因此,它提供了与 vector 十分类似的功能,它不仅可以末尾也可以在序列的前端插入和删除元素。但与 vectors 不同的是,deques 不能保证所有元素都存储在连续的位置:通过偏移指针去访问另一个元素会导致未定义的行为。
vectors 和 deque 提供了非常相似的功能,但是,它们内部的工作方式十分不同。vectors 使用单个数组,需要偶尔重新分配以应对数据的增长,deque 的元素可以被分散到不同的存储块,容器保持必要的信息,提供在常数时间内直接访问任何元素(通过 iterators)。因此,deques 更复杂,对于特别长的序列,相对与重新分配内存,deques 能更加有效的增长。
成员类型
member type definition notes value_type The first template parameter (T) Type of the elements allocator_type The second template parameter (Alloc) defaults to: allocator reference allocator_type::reference for the default allocator: value_type& const_reference allocator_type::const_reference for the default allocator: const value_type& pointer allocator_type::pointer for the default allocator: value_type* const_pointer allocator_type::const_pointer for the default allocator: const value_type* iterator a random access iterator to value_type convertible to const_iterator const_iterator a random access iterator to const value_type reverse_iterator reverse_iterator const_reverse_iterator reverse_iterator difference_type a signed integral type, identical to: iterator_traits::difference_type usually the same as ptrdiff_t size_type an unsigned integral type that can represent any non-negative value of difference_type usually the same as size_t 成员函数
函数 说明 empty Test whether container is empty size Return size max_size Return maximum size resize Change size shrink_to_fit(c++11) Shrink to fit assign Assign container content push_back Add element at the end push_front Insert element at beginning pop_back Delete last element pop_front Delete first element insert Insert elements erase Erase elements swap Swap content clear Clear content emplace(c++11) Construct and insert element emplace_front(c++11) Construct and insert element at beginning emplace_back(c++11) Construct and insert element at the end