stack::stack
描述 (Description)
C ++移动构造函数std::stack::stack()使用移动语义构造具有其他内容的堆栈。
声明 (Declaration)
以下是std :: stack :: stack()构造函数形式std :: stack标头的声明。
C++11
template <class Alloc>
stack (stack&& x, const Alloc& alloc);
参数 (Parameters)
x - 相同类型的堆栈对象。
alloc - 分配器对象。
返回值
构造函数永远不会返回值。
异常 (Exceptions)
该成员函数从不抛出异常。
时间复杂
线性即O(n)
例子 (Example)
以下示例显示了std :: stack :: stack()构造函数的用法。
#include <iostream>
#include <stack>
using namespace std;
int main(void) {
stack<int> s1;
for (int i = 0; i < 5; ++i)
s1.push(i + 1);
cout << "Size of stack s1 before move operation = " << s1.size() << endl;
stack<int> s2(move(s1));
cout << "Size of stack s1 after move operation = " << s1.size() << endl;
cout << "Contents of stack s2" << endl;
while (!s2.empty()) {
cout << s2.top() << endl;
s2.pop();
}
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 -
Size of stack s1 before move operation = 5
Size of stack s1 after move operation = 0
Contents of stack s2
5
4
3
2
1