Home C&C++函数库 c++ 语法 程序源码 Linux C库

std::

stack::stack

公众成员函数  <stack>

C++98;
explicit stack (const container_type& ctnr = container_type());
C++11; initialize (1)
explicit stack (const container_type& ctnr);
move-initialize (2)
explicit stack (container_type&& ctnr = container_type());
allocator (3)
template <class Alloc> explicit stack (const Alloc& alloc);
init + allocator (4)
template <class Alloc> stack (const container_type& ctnr, const Alloc& alloc);
move-init + allocator (5)
template <class Alloc> stack (container_type&& ctnr, const Alloc& alloc);
copy + allocator (6)
template <class Alloc> stack (const stack& x, const Alloc& alloc);
move + allocator (7)
template <class Alloc> stack (stack&& x, const Alloc& alloc);

构建栈

构造一个栈容器适配器对象:

C++98:
容器适配器在内部将容器对象保存为数据。 该容器对象是传递给构造函数的ctnr参数的副本(如果有的话),否则它是一个空容器。

☲  参数


ctnr
容器对象。
container_type是底层容器类型的类型(定义为第二个类模板形参container的别名;见成员类型)。
alloc
分配器对象。
Alloc必须是uses_allocator::value为true的类型(对于其他类型,构造函数甚至不参与重载解析)。
x
相同类型的栈(即具有相同模板参数,T和Container)。

☣  示例



// constructing stacks
#include <iostream>       // std::cout
#include <stack>          // std::stack
#include <vector>         // std::vector
#include <deque>          // std::deque

int main ()
{
  std::deque<int> mydeque (3,100);          // deque with 3 elements
  std::vector<int> myvector (2,200);        // vector with 2 elements

  std::stack<int> first;                    // empty stack
  std::stack<int> second (mydeque);         // stack initialized to copy of deque

  std::stack<int,std::vector<int> > third;  // empty stack using vector
  std::stack<int,std::vector<int> > fourth (myvector);

  std::cout << "size of first: " << first.size() << '\n';
  std::cout << "size of second: " << second.size() << '\n';
  std::cout << "size of third: " << third.size() << '\n';
  std::cout << "size of fourth: " << fourth.size() << '\n';

  return 0;
}

输出:
size of first: 0
size of second: 3
size of third: 0
size of fourth: 2

✥ 复杂度



一个容器构造(对于标准容器而言,其大小达到线性)。

☣ 迭代器的有效性



移动构造函数(2)和(7)可能使所有与moved实参相关的迭代器、指针和引用失效。

⇄ 数据竞争


访问所有复制的元素。动构造函数(2)和(7)可能修改它们的(第一个)参数。

☂ 异常安全性



提供与在容器上执行的操作相同级别的保证。

🍄  另请参阅



stack::push 插入元素(公众成员函数)

联系我们 免责声明 关于CandCplus 网站地图