forward_list ::swap() es una función integrada en CPP STL que intercambia el contenido de la primera forward_list dada con otra forward_list.
Sintaxis:
swap(forward_list first, forward_list second) or forward_list1.swap(forward_list second)
Parámetros: La función acepta dos parámetros que se especifican a continuación:
- primero : la primera lista de reenvío
- segundo : el segundo forward_list
Valor devuelto: La función no devuelve nada. Intercambia ambas listas.
Los siguientes programas demuestran la función mencionada anteriormente:
Programa 1:
// CPP program that demonstrates the // forward_list::swap() function // when two parameters is passed #include <bits/stdc++.h> using namespace std; int main() { // initialising the two forward_list forward_list<int> firstlist = { 9, 8, 7, 6 }; forward_list<int> secondlist = { 10, 20, 30 }; // printing elements before the swap operation // for firstlist and secondlist cout << "Before swap operation firstlist was: "; for (auto it = firstlist.begin(); it != firstlist.end(); ++it) cout << *it << " "; cout << "\nBefore swap operation secondlist was: "; for (auto it = secondlist.begin(); it != secondlist.end(); ++it) cout << *it << " "; // swap operation on two lists swap(firstlist, secondlist); // printing elements after the swap operation // for forward1 and secondlist cout << "\n\nAfter swap operation firstlist is: "; for (auto it = firstlist.begin(); it != firstlist.end(); ++it) cout << *it << " "; cout << "\nAfter swap operation secondlist is:"; for (auto it = secondlist.begin(); it != secondlist.end(); ++it) cout << *it << " "; return 0; }
Producción:
Before swap operation firstlist was: 9 8 7 6 Before swap operation secondlist was: 10 20 30 After swap operation firstlist is: 10 20 30 After swap operation secondlist is:9 8 7 6
Programa 2:
// CPP program that demonstrates the // forward_list::swap() function // when two parameters is passed #include <bits/stdc++.h> using namespace std; int main() { // initialising the two forward_list forward_list<int> firstlist = { 9, 8, 7, 6 }; forward_list<int> secondlist = { 10, 20, 30 }; // printing elements before the swap operation // for firstlist and secondlist cout << "Before swap operation firstlist was: "; for (auto it = firstlist.begin(); it != firstlist.end(); ++it) cout << *it << " "; cout << "\nBefore swap operation secondlist was: "; for (auto it = secondlist.begin(); it != secondlist.end(); ++it) cout << *it << " "; // swap operation on two lists firstlist.swap(secondlist); // printing elements after the swap operation // for forward1 and secondlist cout << "\n\nAfter swap operation firstlist is: "; for (auto it = firstlist.begin(); it != firstlist.end(); ++it) cout << *it << " "; cout << "\nAfter swap operation secondlist is:"; for (auto it = secondlist.begin(); it != secondlist.end(); ++it) cout << *it << " "; return 0; }
Producción:
Before swap operation firstlist was: 9 8 7 6 Before swap operation secondlist was: 10 20 30 After swap operation firstlist is: 10 20 30 After swap operation secondlist is:9 8 7 6
Publicación traducida automáticamente
Artículo escrito por Twinkl Bajaj y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA