简体   繁体   中英

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. 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):

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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