Javatpoint Logo
Javatpoint Logo

C++ List swap()

C++ List swap() function exchange the contents of the list with another list of same type but the sizes can be differ.

Syntax

Parameter

x: It is an another list which is to be exchanged with the given list.

Return value

It does not return any value.

Error:

It throws an error, when both the list are of different type.

Example 1

Let's see a simple example when both the list are of same type and same size.

Output:

Initially,content of list li is : +-*@
Initially,content of list li1 is : java
After swapping, content of list li is : java
After swapping, content of list li1 is : +-*@

In this example, swap() function swaps the contents of list li with the list li1.

Example 2

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

Output:

error : no matching call for list::swap(list&).

In this example, both the list are of different type. Therefore, the function swap() throws an error i.e : no matching call for list::swap(list&).


Next Topicreverse() Function



Help Others, Please Share

facebook twitter pinterest