Javatpoint Logo
Javatpoint Logo

C++ Deque swap()

C++ Deque swap() function exchanges the content of a given deque with the deque passed in the parameter of same type.

Conditions:

  • Type of deque cannot be different.
  • Size of deque can vary.

Syntax

Parameter

second: It is an another deque container whose content is to be swapped with the given deque.

Return value

It does not return any value.

Example 1

Let's see a simple example

Output:

After swapping,value of str is: java is a programming language
After swapping,value of str1 is: C is a programming language

In this example, swap() function swaps the contents of str and str1. Now, str contains "java is a programming language" and str1 contains "C is a programming language".

Example 2

Let's see a simple example when the both the deques are of different type.

Output:

error: no matching function for call to 'std::deque<char>::swap(std::deque<int>&)

In this example, swap() function throws an error as the type of both the deques are different.

Example 3

Let's see a simple example when the sizes are different.

Output:

Content of first deque:10 20 30 40 50 
Content of second deque:1 2 3 4

In this example, swap() function swaps the content of first deque with the second deque.


Next TopicC++ Deque



Please Share

facebook twitter google plus pinterest