C++ Queue 库 - operator= 函数
描述
C++ 函数 std::queue::operator= 通过替换旧内容为队列分配新内容,并在必要时修改大小。
声明
以下是 std::queue::operator= 函数形式的 std::queue 头声明。
C++98
queue<T, Container>& operator=( const queue<T,Container>& other )
参数
other − 另一个相同类型的队列对象。
返回值
返回 this 指针。
异常
此函数从不抛出异常。
时间复杂度
线性,即 O(n)
示例
以下示例显示了 std::queue::operator= 函数的用法。
#include <iostream>
#include <queue>
#include <list>
using namespace std;
int main(void) {
auto it1 = {1, 2, 3, 4, 5};
auto it2 = {10, 20};
queue<int> q1(it1);
queue<int> q2(it2);
q2 = q1;
cout << "Contents of q1" << endl;
while (!q1.empty()) {
cout << q1.front() << endl;
q1.pop();
}
cout << endl;
cout << "Contents of q2" << endl;
while (!q2.empty()) {
cout << q2.front() << endl;
q2.pop();
}
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 −
Contents of q1 1 2 3 4 5 Contents of q2 1 2 3 4 5

