C++ std::list::swap
The std::list::swap function exchanges the contents of two std::list containers. After the swap, the first list contains the elements of the second list, and vice versa. This operation is very efficient because it swaps the internal data structures of the lists without moving or copying individual elements.
Syntax of std::list::swap
void swap(std::list<T>& other);
Parameters
| Parameter | Description |
|---|---|
other | A reference to another std::list container whose contents will be swapped with the current list. |
Return Value
This function does not return a value. It modifies the contents of both lists by exchanging their data structures.
Exceptions
The std::list::swap function does not throw exceptions. It provides a no-throw guarantee since the operation only involves swapping internal pointers or data structures of the lists.
Examples for std::list::swap
Example 1: Swapping Two Lists
This example demonstrates how to use swap to exchange the contents of two lists:
Program
#include <iostream>
#include <list>
int main() {
std::list<int> list1 = {1, 2, 3};
std::list<int> list2 = {10, 20, 30, 40};
// Swap the contents of list1 and list2
list1.swap(list2);
std::cout << "List 1 contents after swap: ";
for (const auto& elem : list1) {
std::cout << elem << " ";
}
std::cout << std::endl;
std::cout << "List 2 contents after swap: ";
for (const auto& elem : list2) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
Explanation:
- Two
std::listcontainers namedlist1andlist2are created with different sets of elements. - The
swapfunction is called to exchange the contents of the two lists. - After the swap,
list1contains the elements oflist2, andlist2contains the elements oflist1. - The program prints the contents of both lists to verify the operation.
Output:
List 1 contents after swap: 10 20 30 40
List 2 contents after swap: 1 2 3
Example 2: Swapping Lists of Different Sizes
This example demonstrates how swap works with lists of different sizes:
Program
#include <iostream>
#include <list>
int main() {
std::list<int> list1 = {5, 10};
std::list<int> list2 = {1, 2, 3, 4, 5};
// Swap the contents of list1 and list2
list1.swap(list2);
std::cout << "List 1 contents after swap: ";
for (const auto& elem : list1) {
std::cout << elem << " ";
}
std::cout << std::endl;
std::cout << "List 2 contents after swap: ";
for (const auto& elem : list2) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
Explanation:
- Two
std::listcontainers,list1andlist2, are created with different sizes and elements. - The
swapfunction is used to exchange the contents of the two lists. - After the swap,
list1contains all the elements oflist2, andlist2contains all the elements oflist1. - The contents of both lists are printed to verify the swap operation.
Output:
List 1 contents after swap: 1 2 3 4 5
List 2 contents after swap: 5 10
Example 3: Using std::swap for Lists
This example demonstrates how to use the std::swap algorithm to swap two lists:
Program
#include <iostream>
#include <list>
#include <algorithm>
int main() {
std::list<int> list1 = {100, 200};
std::list<int> list2 = {300, 400, 500};
// Use std::swap to exchange contents of the lists
std::swap(list1, list2);
std::cout << "List 1 contents after swap: ";
for (const auto& elem : list1) {
std::cout << elem << " ";
}
std::cout << std::endl;
std::cout << "List 2 contents after swap: ";
for (const auto& elem : list2) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
Explanation:
- The
std::swapalgorithm is used to exchange the contents of two lists. - Internally,
std::swapcalls theswapmember function of thestd::listclass. - The contents of both lists are printed to verify the swap operation.
Output:
List 1 contents after swap: 300 400 500
List 2 contents after swap: 100 200
