目录

stack::stack

描述 (Description)

C ++构造函数std::stack::stack()创建堆栈容器,并将ctnr参数的副本分配给堆栈元素。 如果未提供ctnr参数,则构造具有零元素的空堆栈。

声明 (Declaration)

以下是std :: stack :: stack()构造函数形式std :: stack标头的声明。

C++98

explicit stack (const container_type& ctnr = container_type());

C++11

explicit stack (const container_type& ctnr);

参数 (Parameters)

ctnr - 容器类型,它是类模板的第二个参数。

返回值

构造函数永远不会返回值。

异常 (Exceptions)

该成员函数从不抛出异常。

时间复杂

线性即O(n)

例子 (Example)

以下示例显示了std :: stack :: stack()构造函数的用法。

#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int main(void) {   
   stack<int> s1;
   vector<int> v = {1, 2, 3, 4, 5};
   stack<int, vector<int>> s2(v);
   cout << "Size of stack s1 = " << s1.size() << endl;
   cout << "Contents of stack s2" << endl;
   while (!s2.empty()) {
      cout << s2.top() << endl;
      s2.pop();
   }
   return 0;
}

让我们编译并运行上面的程序,这将产生以下结果 -

Size of stack s1 = 0
Contents of stack s2
5
4
3
2
1
↑回到顶部↑
WIKI教程 @2018