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.
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 throws an error if the lists are not of the same size.
2. It has a basic no exception throw guarantee otherwise.
// 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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- std::sort() in C++ STL
- Kruskal's Minimum Spanning Tree using STL in C++
- Dijkstra’s shortest path algorithm using set in STL
- Dijkstra's Shortest Path Algorithm using priority_queue of STL
- Prim's algorithm using priority_queue in STL
- unordered_map in C++ STL
- fill() and fill_n() functions in C++ STL
- std::transform() in C++ STL (Perform an operation on all elements)
- Count number of unique Triangles using STL | Set 1 (Using set)
- Permutations of a given string using STL
- Algorithm Library | C++ Magicians STL Algorithm
- Array algorithms in C++ STL (all_of, any_of, none_of, copy_n and iota)
- Iterators in C++ STL
- accumulate() and partial_sum() in C++ STL : numeric header
- std::partition in C++ STL
- Merge operations using STL in C++ | merge(), includes(), set_union(), set_intersection(), set_difference(), ., inplace_merge,
- Heap in C++ STL | make_heap(), push_heap(), pop_heap(), sort_heap(), is_heap, is_heap_until()
- numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota())
- Counts of distinct consecutive sub-string of length two using C++ STL
- Graph implementation using STL for competitive programming | Set 1 (DFS of Unweighted and Undirected)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

