C++ List::swap() 函式



C++ 的std::list::swap()函式用於交換列表的內容與其他列表的內容。它不會對單個列表元素呼叫任何移動、複製或交換操作。

在 C++ 中,交換隻不過是一種交換功能,用於將資料與另一個數據交換。swap() 函式在交換列表元素時會忽略列表大小,但會驗證列表型別。如果列表型別不同,它將引發錯誤。此函式的返回型別為 void,這意味著它不返回任何值。它根據需要更改列表大小。

語法

以下是 C++ std::list::swap() 函式的語法:

void swap( list& other );

引數

other − 它是另一個相同型別的列表物件。

返回值

此函式不返回任何值。

示例 1

在下面的程式中,我們使用 C++ std::list::swap() 函式將指定的列表(int 型別)元素 {1, 2, 3, 4, 5} 與當前列表元素 {10, 20, 30, 40} 交換。

#include<iostream>
#include<list>
using namespace std;

int main() {
   //create an int list
   list<int> list1 = {10, 20, 30, 40};
   cout<<"First list elements before swap: "<<endl;
   for(int l1 : list1) {
      cout<<l1<<" ";
   }
   list<int> list2 = {1, 2, 3, 4, 5};
   cout<<"\nSecond list elements before swap: "<<endl;
   for(int l2: list2) {
      cout<<l2<<" ";
   }
   //using the swap() function
   list1.swap(list2);
   cout<<"\nFirst list elements after swap: "<<endl;
   for(int l1 : list1) {
      cout<<l1<<" ";
   }
   cout<<"\nSecond list elements after swap: "<<endl;
   for(int l2 : list2) {
      cout<<l2<<" ";
   }
}

輸出

以下是上述程式的輸出:

First list elements before swap: 
10 20 30 40 
Second list elements before swap: 
1 2 3 4 5 
First list elements after swap: 
1 2 3 4 5 
Second list elements after swap: 
10 20 30 40 

示例 2

以下是 C++ std::list::swap() 函式的另一個示例。在這裡,我們建立了兩個名為 upper_case_list 和 lower_case_list 的列表(char 型別),其值為 {'A', 'B', 'C', 'D'} 和 {'a', 'b', 'c', 'd'}。然後,使用 swap() 函式,我們嘗試將 upper_case_list 元素與 lower_case_list 元素交換。

#include<iostream>
#include<list>
using namespace std;

int main() {
   //create list
   list<char> upper_case_list  = {'A', 'B', 'C', 'D'};
   cout<<"First list elements before swap: "<<endl;
   for(char u : upper_case_list) {
      cout<<u<<" ";
   }
   list<char> lower_case_list  = {'a', 'b', 'c', 'd'};
   cout<<"\nSecond list elements before swap: "<<endl;
   for(char l: lower_case_list) {
      cout<<l<<" ";
   }
   //using the swap() function
   upper_case_list.swap(lower_case_list);
   cout<<"\nFirst list elements after swap: "<<endl;
   for(char u : upper_case_list) {
      cout<<u<<" ";
   }
   cout<<"\nSecond list elements after swap: "<<endl;
   for(char l : lower_case_list) {
      cout<<l<<" ";
   }
}

輸出

執行上述程式後,將產生以下輸出:

First list elements before swap: 
A B C D 
Second list elements before swap: 
a b c d 
First list elements after swap: 
a b c d 
Second list elements after swap: 
A B C D 

示例 3

除了 int 和 char 列表元素外,您還可以交換列表中的字串元素(string 型別)。

在此示例中,我們建立了兩個名為 names 和 surnames 的列表(string 型別),其值為 {"Rahul", "Geeta", "Reeta"} 和 {"Verma", "Gupta", "Sharma"}。然後,使用 swap() 函式,我們嘗試將 name 列表元素與 surnames 列表元素交換。

#include<iostream>
#include<list>
using namespace std;

int main() {
   //create list
   list<string> names  = {"Rahul", "Geeta", "Reeta"};
   cout<<"First list elements before swap: "<<endl;
   for(string n : names){
      cout<<n<<" ";
   }
   list<string> surnames  = {"Verma", "Gupta", "Sharma"};
   cout<<"\nSecond list elements before swap: "<<endl;
   for(string s: surnames){
      cout<<s<<" ";
   }
   //using the swap() function
   names.swap(surnames);
   cout<<"\nFirst list elements after swap: "<<endl;
   for(string n : names){
      cout<<n<<" ";
   }
   cout<<"\nSecond list elements after swap: "<<endl;
   for(string s : surnames){
      cout<<s<<" ";
   }
}

輸出

這將生成以下輸出:

First list elements before swap: 
Rahul Geeta Reeta 
Second list elements before swap: 
Verma Gupta Sharma 
First list elements after swap: 
Verma Gupta Sharma 
Second list elements after swap: 
Rahul Geeta Reeta 
廣告
© . All rights reserved.