目录

stack::operator=

描述 (Description)

C ++函数std::stack::operator=通过替换旧的内容将新内容分配给堆栈。 如有必要,此方法会修改堆栈的大小。

声明 (Declaration)

以下是std :: stack :: operator = function形式std :: stack标头的声明。

C++98

stack<T, Container>& 
operator=(const stack<T,Container>& x );

参数 (Parameters)

x - 相同类型的另一个堆栈对象。

返回值

返回此指针。

异常 (Exceptions)

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

时间复杂

线性即O(n)

例子 (Example)

以下示例显示了std :: stack :: operator = function的用法。

#include <iostream>
#include <stack>
using namespace std;
int main(void) {
   stack<int> s1;
   stack<int> s2;
   for (int i = 0; i < 5; ++i)
      s1.push(i + 1);
   s2 = s1;
   cout << "Contents of stack s2" << endl;
   while (!s2.empty()) {
      cout << s2.top() << endl;
      s2.pop();
   }
   return 0;
}

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

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