The Wayback Machine - https://web.archive.org/web/20241126020807/https://www.geeksforgeeks.org/listswap-c-stl/
Open In App

list::swap() in C++ STL

Last Updated : 28 Feb, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists.

list::swap()

This function is used to swap the contents of one list with another list of same type and size.

Syntax :

listname1.swap(listname2)
Parameters :
The name of the lists with which
the contents have to be swapped.
Result :
All the elements of the 2 list are swapped.

Examples:

Input  : mylist1 = {1, 2, 3, 4}
mylist2 = {3, 5, 7, 9}
mylist1.swap(mylist2);
Output : mylist1 = {3, 5, 7, 9}
mylist2 = {1, 2, 3, 4}
Input : mylist1 = {1, 3, 5, 7}
mylist2 = {2, 4, 6, 8}
mylist1.swap(mylist2);
Output : mylist1 = {2, 4, 6, 8}
mylist2 = {1, 3, 5, 7}

Errors and Exceptions

1. It throws an error if the lists are not of the same type.

2. It has a basic no exception throw guarantee otherwise. 

CPP




// CPP program to illustrate
// Implementation of swap() function
#include <iostream>
#include <list>
using namespace std;
 
int main()
{
 // list container declaration
 list<int> mylist1{ 1, 2, 3, 4 };
 list<int> mylist2{ 3, 5, 7, 9 };
 
 // using swap() function to
 //swap elements of lists
 mylist1.swap(mylist2);
 
 // printing the first list
 cout << "mylist1 = ";
 for (auto it = mylist1.begin();
   it != mylist1.end(); ++it)
  cout << ' ' << *it;
 
 // printing the second list
 cout << endl
  << "mylist2 = ";
 for (auto it = mylist2.begin();
   it != mylist2.end(); ++it)
  cout << ' ' << *it;
 return 0;
}


Output:

mylist1 = 3 5 7 9 
mylist2 = 1 2 3 4

Time Complexity: O(n)
Auxiliary Space: O(1)



Previous Article
Next Article

Similar Reads

Difference between std::swap and std::vector::swap
The std::swap is a general function used to exchange the given values whereas the std::vector::swap is a specialized function that can swap all the contents of two different vector containers.Below are some major key differences between std::swap and std::vector::swap, std::swapstd::vector::swapThe std::swap() is a built-in function in C++ STL whic
3 min read
deque::at() and deque::swap() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::at()at() function is us
4 min read
array::fill() and array::swap() in C++ STL
Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::fill() This function is used to set a common value for all the elements of the array container. Syntax : arrayname.fill(value) Parameters : The value to be
3 min read
forward_list::swap() in C++ STL
Forward list in STL implements singly linked list. Introduced from C++11, forward lists are useful than other containers for insertion, removal and moving operations (like sort) and allows time constant insertion and removal of elements.It differs from the list by the fact that forward list keeps track of the location of only next element while lis
2 min read
queue::swap() in C++ STL
Queue is also an abstract data type or a linear data structure, which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). In a FIFO data structure, the first element added to the queue will be the first one to be removed. queue::swap() swap() function is used to exchange the contents of two queu
2 min read
set::swap() in C++ STL
Sets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. set::swap() This function is used to exchange the contents of two sets
2 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 smallest element at the top by simply passing an extr
3 min read
multiset::swap() in C++ STL
Multisets are a type of associative containers similar to set, with an exception that multiple elements can have same values. multiset::swap() This function is used to exchange the contents of two multisets but the sets must be of same type, although sizes may differ. Syntax : multisetname1.swap(multisetname2) Parameters : The name of the multiset
3 min read
multimap::swap() in C++ STL
multimap::swap() is used to swap the contents of one multimap with another multimap of same type and size. Syntax:- multimap1.swap(multimap2) Parameters : The name of the multimap with which the contents have to be swapped. Result : All the elements of the 2 multimap are swapped. Examples: Input: multimap1 = { ('a',1), ('b',2), ('c',3) multimap2 =
2 min read
map::at() and map::swap() in C++ STL
Maps are the container in STL which is used to store the elements in the form of key-value pair. Internally, the elements in a map are always sorted by its key. Maps are mainly implemented as binary search trees. map::at() at() function is used to return the reference to the element associated with the key k. Syntax: map1.at(k) Parameters: k is the
3 min read
forward_list::swap() in C++ STL
The forward_list::swap() is a built-in function in CPP STL which exchanges the contents of the first given forward_list with another forward_list. Syntax: swap(forward_list first, forward_list second) or forward_list1.swap(forward_list second) Parameters: The function accepts two parameters which are specified as below: first - The first forward_li
3 min read
multimap swap() function in C++ STL
The multimap::swap() is a built-in function in C++ STL which swaps two multimap container. The contents of multimap1 are in multimap2 and contents of multimap2 are in multimap1 after swap() function is called. Syntax: multimap1_name.swap(multimap2_name) Parameters: This function accepts one parameter which is to be swapped with the multimap1_name,
2 min read
unordered_set swap() function in C++ STL
The unordered_set::swap() method is a builtin function in C++ STL which is used to exchange values of two unordered_set containers. It swaps the element of two unordered_set containers. The sizes may differ but it swaps elements and changes the order of elements also.Syntax: unordered_set_firstname.swap(unordered_set_secondname) Parameter: The func
2 min read
unordered_multimap swap() function in C++ STL
The unordered_multimap::swap() is a built-in function in C++ STL which swaps the contents of two unordered_multimap containers. The sizes can differ of both the containers. Syntax: unordered_multimap_name1.swap(unordered_multimap_name2) Parameters: The function accepts a single mandatory parameter unordered_multimap_name2 which specifies the unorde
3 min read
unordered_set swap() in C++ STL
The swap() method of “unordered_set” swaps the contents of two containers. It is public member function. This function: Exchanges the content of the container by the content of variable, which is another unordered_set object containing elements of the same type but the sizes may differ. After the call to this member function, the elements in this c
3 min read
unordered_multiset swap() function in C++ STL
The unordered_multiset::swap() is a built-in function in C++ STL which swaps the contents of two unordered_multiset containers. Note: Both of the containers should have the same type of elements. Sizes of the containers may differ. Syntax: unordered_multiset1.swap(unordered_multiset2); Parameters: The function accepts only one compulsory parameter
3 min read
unordered_multiset swap() in C++ STL
The swap() method of “unordered_multiset” swaps the contents of two containers. It is public member function. This function: Exchanges the content of the container by the content of variable, which is another unordered_multiset object containing elements of the same type but the sizes may differ. After the call to this member function, the elements
3 min read
unordered_map swap in C++ STL
The std::unordered_map::swap() is a built in function in C++ STL which swaps the elements of a container to an other container. After the call of this function elements of the caller unordered_map will be elements of called unordered_map while elements of called unordered_map will be elements of caller unordered_map. Internally swapping of elements
4 min read
stack swap() in C++ STL
Stacks are a type of container adaptors with LIFO(Last In First Out) type of work, where a new element is added at one end and (top) an element is removed from that end only. stack::swap()This function is used to swap the contents of one stack with another stack of same type but the size may vary. Syntax : stackname1.swap(stackname2)Parameters: The
2 min read
list::push_front() and list::push_back() in C++ STL
Lists are containers used in C++ to store data in a non-contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::push_front() push_front() function is used to push elements into a list from the front. The n
4 min read
list::pop_front() and list::pop_back() in C++ STL
Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::pop_front() pop_front() function is used to pop or remove elements from a list from the front
4 min read
list::front() and list::back() in C++ STL
Lists are containers used in C++ to store data in a non-contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::front() This function is used to reference the first element of the list container. This func
3 min read
list::empty() and list::size() in C++ STL
Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::empty()empty() function is used to check if the list container is empty or not. Syntax : list
3 min read
list::remove() and list::remove_if() in C++ STL
Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::remove() remove() function is used to remove all the values from the list that correspond to
3 min read
list::emplace_front() and list::emplace_back() in C++ STL
Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::emplace_front()This function is used to insert a new element into the list container, the new
3 min read
list::begin() and list::end() in C++ STL
Lists are containers used in C++ to store data in a non-contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::begin() begin() function is used to return an iterator pointing to the first element of the l
3 min read
basic_istream::swap() in C++ with Examples
The basic_ios::swap(x) is used swap all data members of the base class except for rdbuf(), and swaps the values of the gcount() counters between *this and x. This basic_ios::swap(x) function is a protected function. Below is the syntax and the header file for the same: Header File: #include<iostream> Syntax: void swap (basic_istream& x);
2 min read
Swap Two Variables in One Line
We have discussed different approaches to swap two integers without the temporary variable. How to swap into a single line without using the library function?1) Python: In Python, there is a simple and syntactically neat construct to swap variables, we just need to write "x, y = y, x".2) C/C++: Below is one generally provided classical solution: //
4 min read
Copy-and-Swap Idiom in C++
Before going into deep let us first have a look on the normal 'overloaded assignment operator' that we use. C/C++ Code // Simple C++ program to demonstrate overloading of // assignment operator. #include <cstring> #include <iostream> using namespace std; class anyArrayClass { int size; int* ptr; public: // Initializer list anyArrayClass
3 min read
How to Overload std::swap()?
In C++, std::swap is a standard library function that is used to exchange the values of two objects. There might be situations where we want to provide a custom implementation for our user-defined types to improve performance or handle special cases for such cases we need to overload std::swap. In this article, we will learn how to overload std::sw
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg