共用方式為


list::swap

交換兩個清單的項目。

void swap( 
   list<Type, Allocator>& _Right 
); 
friend void swap( 
   list<Type, Allocator>& _Left, 
   list<Type, Allocator>& _Right 
)

參數

  • _Right
    提供待交換項目的清單,或其項目與清單 _Left 交換的清單。

  • _Left
    其項目會被其他 _Right清單交換的清單。

範例

// list_swap.cpp
// compile with: /EHsc
#include <list>
#include <iostream>

int main( )
{
   using namespace std;
   list <int> c1, c2, c3;
   list <int>::iterator c1_Iter;

   c1.push_back( 1 );
   c1.push_back( 2 );
   c1.push_back( 3 );
   c2.push_back( 10 );
   c2.push_back( 20 );
   c3.push_back( 100 );

   cout << "The original list c1 is:";
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
      cout << " " << *c1_Iter;
   cout << endl;

   c1.swap( c2 );

   cout << "After swapping with c2, list c1 is:";
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
      cout << " " << *c1_Iter;
   cout << endl;

   swap( c1,c3 );

   cout << "After swapping with c3, list c1 is:";
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
      cout << " " << *c1_Iter;
   cout << endl;
}
  

需求

標題: <list>

命名空間: std

請參閱

參考

list 類別

標準樣板程式庫