當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ forward_list::swap()用法及代碼示例


forward_list::swap()是CPP STL中的內置函數,它與另一個forward_list交換第一個給定的forward_list的內容。

用法:

swap(forward_list first, forward_list second)
               or 
forward_list1.swap(forward_list second)

參數:該函數接受兩個指定如下的參數:


  • first-第一個forward_list
  • second-第二個forward_list

返回值:該函數不返回任何內容。它交換兩個列表。

下麵的程序演示了上述函數:

程序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; 
}
輸出:
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

程序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; 
}
輸出:
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


相關用法


注:本文由純淨天空篩選整理自Twinkl Bajaj大神的英文原創作品 forward_list::swap() in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。