priority_queue::swap() in C++ STL
Last Updated : 17 Jan, 2023
Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue. However, in C++ STL (by default) the largest element is at the top. We can also create a priority queue having the smallest element at the top by simply passing an extra parameter while creating the priority queue.
priority_queue::swap()
This function is used to swap the contents of one priority queue with another priority queue of same type and same or different size.
Time complexity: O(1)
Syntax :
priorityqueuename1.swap(priorityqueuename2) Parameters : The name of the priority queue with which the contents have to be swapped. Result : All the elements of the 2 priority queues are swapped.
Examples:
Input : mypqueue1 = {1, 2, 3, 4} mypqueue2 = {3, 5, 7, 9} mypqueue1.swap(mypqueue2); Output : mypqueue1 = {9, 7, 5, 3} mypqueue2 = {4, 3, 2, 1} Input : mypqueue1 = {1, 3, 5, 7} mypqueue2 = {2, 4, 6, 8} mypqueue1.swap(mypqueue2); Output : mypqueue1 = {8, 6, 4, 2} mypqueue2 = {7, 5, 3, 1}
Note: In priority_queue container, the elements are printed in reverse order because the top is printed first then moving on to other elements.
Errors and Exceptions 1. It throws an error if the priority queues are not of the same type. 2. It has a basic no exception throw guarantee otherwise.
Example 1: It swaps two priority queue of same type and same size
CPP // CPP program to illustrate // Implementation of swap() function #include <iostream> #include <queue> using namespace std; int main() { // priority_queue container declaration priority_queue<int> mypqueue1; priority_queue<int> mypqueue2; // pushing elements into the 1st priority queue mypqueue1.push(1); mypqueue1.push(2); mypqueue1.push(3); mypqueue1.push(4); // pushing elements into the 2nd priority queue mypqueue2.push(3); mypqueue2.push(5); mypqueue2.push(7); mypqueue2.push(9); // using swap() function to swap elements of priority queues mypqueue1.swap(mypqueue2); // printing the first priority queue cout << "mypqueue1 = "; while (!mypqueue1.empty()) { cout << mypqueue1.top() << " "; mypqueue1.pop(); } // printing the second priority queue cout << endl << "mypqueue2 = "; while (!mypqueue2.empty()) { cout << mypqueue2.top() << " "; mypqueue2.pop(); } return 0; }
Outputmypqueue1 = 9 7 5 3 mypqueue2 = 4 3 2 1
Example 2: It swaps two priority queue of same type but different size
C++ // CPP program to illustrate // Implementation of swap() function // for different sizes #include <bits/stdc++.h> using namespace std; int main() { // priority_queue container declaration priority_queue<int> mypqueue1; priority_queue<int> mypqueue2; // pushing elements into the 1st priority queue mypqueue1.push(1); mypqueue1.push(2); mypqueue1.push(3); mypqueue1.push(4); mypqueue1.push(5); // pushing elements into the 2nd priority queue mypqueue2.push(3); mypqueue2.push(5); mypqueue2.push(7); mypqueue2.push(9); // using swap() function to swap elements of priority queues mypqueue1.swap(mypqueue2); // printing the first priority queue cout<<"mypqueue1 = "; while (!mypqueue1.empty()) { cout<<mypqueue1.top() << " "; mypqueue1.pop(); } // printing the second priority queue cout<<endl<<"mypqueue2 = "; while (!mypqueue2.empty()) { cout<<mypqueue2.top() << " "; mypqueue2.pop(); } return 0; } // This code is contributed by Susobhan Akhuli
Outputmypqueue1 = 9 7 5 3 mypqueue2 = 5 4 3 2 1
Similar Reads
Priority Queue in C++ STL In C++, priority queue is a type of queue in which there is some priority assigned to the elements. According to this priority, elements are removed from the queue. By default, the value of the element being inserted is considered as priority. Higher its value, higher its priority. But this can be c
6 min read
priority_queue::push() and priority_queue::pop() in C++ STL In C++, priority_queue::push() and priority_queue::pop() methods are used to insert and delete the element from the priority_queue container. They both are the member functions of std::priority_queue class defined inside <queue> header file. In this article, we will learn about priority_queue:
2 min read
priority_queue::top() in C++ STL Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue. In general, elements are arranged according to some priority. However in C++ STL, the top element is the greatest elem
3 min read
priority_queue::empty() and priority_queue::size() in C++ STL Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue. However, in C++ STL (by default) the largest element is at the top. We can also create a priority queue having the sma
4 min read
priority_queue emplace() in C++ STL Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue. However, in C++ STL (by default) the largest element is at the top. We can also create a priority queue having the sma
4 min read
priority_queue::swap() in C++ STL Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue. However, in C++ STL (by default) the largest element is at the top. We can also create a priority queue having the sma
3 min read
priority_queue value_type in C++ STL The priority_queue :: value_type method is a built-in function in C++ STL which represents the type of object stored as an element in a priority_queue. It acts as a synonym for the template parameter. Time complexity: O(1)Syntax: priority_queue::value_type variable_name It has no parameters and no r
2 min read
Priority Queue of Sets in C++ with Examples Priority Queues Priority queues are a type of container adapters, specifically designed such that the first element of the queue is the greatest of all elements in the queue and elements are in nonincreasing order (hence we can see that each element of the queue has a priority {fixed order}). Functi
4 min read
Priority queue of pairs in C++ with ordering by first and second element Priority Queue: Priority queue is the extension of the queue in which elements associated with priority and elements having higher priority is popped first. Priority queue can contain elements with various data types such as integer, pair of integers, custom data type. But one thing is common that t
5 min read
Multiple comparisons in a C++ priority queue? What is a Priority Queue? A Priority Queue is an abstract data type that is similar to a queue, and every element has some priority value associated with it. The priority of the elements in a priority queue determines the order in which elements are served (i.e., the order in which they are removed)
5 min read