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::list
containers namedlist1
andlist2
are created with different sets of elements. - The
swap
function is called to exchange the contents of the two lists. - After the swap,
list1
contains the elements oflist2
, andlist2
contains 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::list
containers,list1
andlist2
, are created with different sizes and elements. - The
swap
function is used to exchange the contents of the two lists. - After the swap,
list1
contains all the elements oflist2
, andlist2
contains 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::swap
algorithm is used to exchange the contents of two lists. - Internally,
std::swap
calls theswap
member function of thestd::list
class. - 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