dimanche 22 juillet 2018

What is the use of swap() function in priority_queue class in C++?

I was exploring priority_queue class in C++ and couldn't think about any practical use of swap().

Ref: http://www.cplusplus.com/reference/queue/priority_queue/swap/

Can someone list a practical example where it is beneficial to use this function. Essentially it feels like renaming the priority queue variable.

std::priority_queue<int> foo,bar;
foo.push (15); foo.push(30); foo.push(10);
bar.push (101); bar.push(202);

foo.swap(bar);

std::cout << "size of foo: " << foo.size() << '\n';
std::cout << "size of bar: " << bar.size() << '\n';

Aucun commentaire:

Enregistrer un commentaire