Javatpoint Logo
Javatpoint Logo

C++ Algorithm Functions swap()

C++ Algorithm swap() function swaps or say interchanges the values of two containers under reference.



a: It is the first container with some value.

b: It is another container with some value.

Return value

The function only swaps the values of two containers and does not return something.

Example 1


sg contains: 14 14 14 14 14 14

Example 2


Value of ss before swapping: 9
Value of sg before swapping: 14
Value of ss after swapping: 14
Value of sg after swapping: 9


For arrays the function has N complexity as the operation of swapping is individually performed on each element. For non array the function has constant complexity.

Data races

Both the containers are undergo modification


The function throws an exception if any of the container elements throws one.

Next Topic

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA