小工具知识网

当前位置: 首页 > 行业知识 > 常用的C++ STL

行业知识

常用的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 typedefinitionnotes
      value_typeThe first template parameter (T)Type of the elements
      container_typeThe second template parameter (Container)Type of the underlying container
      size_typean unsigned integral typeusually the same as size_t

      成员函数

      函数说明
      emptyTest whether container is empty
      sizeReturn size
      topAccess next element
      pushInsert element
      popRemove 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 typedefinitionnotes
      value_typeThe first template parameter (T)Type of the elements
      container_typeThe second template parameter (Container)Type of the underlying container
      size_typean unsigned integral typeusually the same as size_t

      成员函数

      函数说明
      emptyTest whether container is empty
      sizeReturn size
      frontAccess next element
      backAccess last element
      pushInsert element
      popRemove 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 typedefinitionnotes
      value_typeThe first template parameter (T)Type of the elements
      allocator_typeThe second template parameter (Alloc)defaults to: allocator
      referenceallocator_type::referencefor the default allocator: value_type&
      const_referenceallocator_type::const_referencefor the default allocator: const value_type&
      pointerallocator_type::pointerfor the default allocator: value_type*
      const_pointerallocator_type::const_pointerfor the default allocator: const value_type*
      iteratora random access iterator to value_typeconvertible to const_iterator
      const_iteratora random access iterator to const value_type
      reverse_iteratorreverse_iterator
      const_reverse_iteratorreverse_iterator
      difference_typea signed integral type, identical to: iterator_traits::difference_typeusually the same as ptrdiff_t
      size_typean unsigned integral type that can represent any non-negative value of difference_typeusually the same as size_t

      成员函数

      函数说明
      emptyTest whether container is empty
      sizeReturn size
      max_sizeReturn maximum size
      resizeChange size
      shrink_to_fit(c++11)Shrink to fit
      assignAssign container content
      push_backAdd element at the end
      push_frontInsert element at beginning
      pop_backDelete last element
      pop_frontDelete first element
      insertInsert elements
      eraseErase elements
      swapSwap content
      clearClear 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

上一篇:在云南怎么办理异地就医备案登记

下一篇:仙境传说Account权限揭秘,GM权限获取警示与违法行为解析