0

I want to implement a some functionality, where I will be needing two queues from STL. And I frequently have to do some operations based on input data,and I may need to move all elements from one queue to another and vice versa.But I am thinking that if i maintain reference to two queues,then I can just swap references instead of two queues.So is it possible in C++ that I have two references to two queues, and i can swap those references as per requirement ?How can i do it if possible?Normally i would have done as follow:

Queue<int> Q1;
Queue<int> Q2;
I can swap like:
Queue<int> Q3=Q1;
Q1=Q2;
Q2=Q3;

But it will do entire data copy and can be a very time consuming operation.How can i represent Q1 and Q2 as reference and then perform just swap of reference?

rahul sharma
  • 255
  • 2
  • 12
  • 3
    What about `std::swap`? – vahancho Aug 26 '20 at 13:48
  • 1
    _"...and i can swap those references..."_ references can't be rebound; however pointers can. – Richard Critten Aug 26 '20 at 13:49
  • @RichardCritten `std::reference_wrapper` can also be used to have a non-null, rebindable reference. – Simon Kraemer Aug 26 '20 at 13:53
  • yes,my bad.References cannot be re bounded.One way is to have my custom queue class and i can have pointer to those queues and then can easily swap two pointer variables.But i was looking if I can somehow have the same thing with stl containers?Will std:swap internally move data from one queue to another ? – rahul sharma Aug 26 '20 at 13:53
  • Yes, `std::swap` will move the containers, so there won't be any copies. – cigien Aug 26 '20 at 13:55
  • i mean using std:swap,will it actually move one queue data to another like the in mentioned in snippet in code?Because i am looking to avoid actually data copy and somehow wanted to swap the references/pointers to queue so that it can be done in constant time.Is it possible using STL queues?I can anyhow do this with custom queues as i mentioned because above – rahul sharma Aug 26 '20 at 13:59
  • `std::swap` will do that for you. But you should probably try it before you ask questions? – Thrasher Aug 26 '20 at 14:01
  • Yes, it'll work with custom containers, and do moves efficiently, but you have to write an efficient move for your class. – cigien Aug 26 '20 at 14:01
  • with custom implementation of queue,I can represent my to queue as two pointers Queue * Q1 and Queue * Q2 and whenever I need to swap two queues,I can just swap Q1 and Q2 pointers only and get it done.There are no moves as i swapped only two pointers.@Thrasher actually std:swap will do the work if i try but i wanted to be sure that there should be no actual movement of data.The way i can swap two pointers in custom implementation and get it done.I was looking same solution in C++ STL Queue. – rahul sharma Aug 26 '20 at 14:04
  • @rahulsharma Well, `std::queue` supports full move semantics so you should be ok. – Paul Sanders Aug 26 '20 at 14:10
  • For pre-C++11 there is also a [`swap()` member function](https://stackoverflow.com/questions/42718327/why-does-every-stl-container-have-a-swap-function-defined-as-a-member-function). – rustyx Aug 26 '20 at 14:14

1 Answers1

1

Don't try to be too smart. Just use the straightforward way - std::swap. std::queue is movable, so you won't copy anything, it is already very efficient. Just to ensure this is the case with your own Queue, add assertions:

static_assert(std::is_move_constructible_v<Queue<int>> && std::is_move_assignable_v<<Queue<int>>)

If this is too slaw for your particular case then you can try to come up with something more fancy. But do not try to optimize (especially microoptimize) without benchmarks, because most probable you will fail. With the today's hardware complexity and with the intelligence of today's compilers it very hard to reason about code efficiency without actual benchmarks. Once again, choose a simply way, if this isn't enough (you can know this only by testing/benchmarking) then try something more complicated.

nicolai
  • 1,140
  • 9
  • 17