简体   繁体   English

在变量之间移动队列而不复制元素

[英]Move Queues between variables without copying elements

I have several queues in an application, and I want to change where they are 'pointing' to, changing ones for the others. 我在一个应用程序中有多个队列,我想更改它们“指向”的位置,为其他队列更改。

For example, if I have: 例如,如果我有:

queue<int> q1 = queue<int>();
queue<int> q2 = queue<int>();
queue<int> q3 = queue<int>();

There are several elements inside each of the queue, and I want to update q1, making it point to q2, and q2, making it point to q3, instead of copying the elements. 每个队列中都有几个元素,我想更新q1,使其指向q2,更新q2,使其指向q3,而不是复制元素。 Can I do something like: 我可以做类似的事情吗?

q1 = &q2;
q2 = &q3;
q3 = queue<int>();

If so, how should I write that? 如果是这样,我应该怎么写? Or do I have to transform those queues into pointers to queues, to be able to change where they are pointing to? 还是我必须将这些队列转换为指向队列的指针,以便能够更改它们指向的位置?

I would rather avoid pointers so I do not have to manage the memory explicitly. 我宁愿避免使用指针,因此不必显式管理内存。

Instead of copy, you may move elements (since C++11): 您可以移动元素(而不是复制)(自C ++ 11起):

q1 = std::move(q2);
q2 = std::move(q3);
q3 = queue<int>();

so q1 holds previous resources from q2 , q2 holds previous resources from q3 and q3 is empty. 所以q1从以前保存资源q2q2从以前保存资源q3q3是空的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM