A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/cpp_standard_library/cpp_queue_swap_function.htm below:

C++ Queue::swap() Function

C++ Queue::swap() Function

The C++ std::queue::swap() function is used to exchange the contents of two queues. It operates on queue containing the same type of elements. It provides a efficient way to swap elements without copying them individually.

The swap() function can be called in two ways: as a member function or as a non-member function. The time complexity of the swap() when used as member function is Constant i.e.O(1) orelse it is linear i.e.O(n) when used as non-member function. you can find the syntaxes of both the ways below.

Syntax

Following is the syntax for std::queue::swap() function.

void swap (queue& x) noexcept;
or
void swap (queue<T,Container>& q1, queue<T,Container>& q2) noexcept;
Parameters Return value

This function does not return anything.

Example

Let's look at the following example, where we are going to swap the contents of two integer queues.

#include <iostream>
#include <queue>
int main()
{
    std::queue<int> a, b;
    for(int x = 1; x <= 4; ++x)
        a.push(x);
    for(int x = 11; x <= 14; ++x)
        b.push(x);
    a.swap(b);
    std::cout << "Queue 1: ";
    while (!a.empty()) {
        std::cout << a.front() << " ";
        a.pop();
    }
    std::cout << "\nQueue 2: ";
    while (!b.empty()) {
        std::cout << b.front() << " ";
        b.pop();
    }
    return 0;
}
Output

Output of the above code is as follows −

Queue 1: 11 12 13 14 
Queue 2: 1 2 3 4 
Example

Consider the another scenario, where we are going to swap the content of two string queues.

#include <iostream>
#include <queue>
#include <string>
int main()
{
    std::queue<std::string> x, y;
    x.push("AB");
    x.push("BC");
    x.push("CD");
    y.push("DE");
    y.push("EF");
    x.swap(y);
    std::cout << "Queue 1 : ";
    while (!x.empty()) {
        std::cout << x.front() << " ";
        x.pop();
    }
    std::cout << "\nQueue 2 : ";
    while (!y.empty()) {
        std::cout << y.front() << " ";
        y.pop();
    }
    return 0;
}
Output

Following is the output of the above code −

Queue 1 : DE EF 
Queue 2 : AB BC CD 
Example

In the following example, we are going to swap the empty queue with non empty queue and observing the output.

#include <iostream>
#include <queue>
int main()
{
    std::queue<int> a, b;
    a.push(11);
    a.push(222);
    a.swap(b);
    std::cout << "Queue 1: ";
    while (!a.empty()) {
        std::cout << a.front() << " ";
        a.pop();
    }
    std::cout << "\nQueue 2: ";
    while (!b.empty()) {
        std::cout << b.front() << " ";
        b.pop();
    }
    return 0;
}
Output

If we run the above code it will generate the following output −

Queue 1: 
Queue 2: 11 222 
Example

Following is the example, where we are going to swap the empty queues and retrive the size of the queue after swapping.

#include <iostream>
#include <queue>
int main()
{
    std::queue<int> a;
    std::queue<int> b;
    a.swap(b);
    std::cout << "Size of queue1 after swap: " << a.size() << std::endl;
    std::cout << "Size of queue2 after swap: " << b.size() << std::endl;
    return 0;
}
Output

Following is the output of the above code −

Size of queue1 after swap: 0
Size of queue2 after swap: 0

queue.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4