共用方式為


swap (C++ STL <string>)

交換兩個字串的字元陣列。

template<class Traits, class Allocator>    void swap(       basic_string<CharType, Traits, Allocator>& _Left,       basic_string<CharType, Traits, Allocator>& _Right     );

參數

  • _Left
    一個字串,其元素要與另一個字串的元素進行交換。

  • _Right
    另一個字串,其元素要與第一個字串交換。

備註

範本函式會為字串執行特殊的成員函式 _Left.swap(_Right),這可以保證常數的複雜性。

範例

// string_swap.cpp
// compile with: /EHsc
#include <string>
#include <iostream>

int main( ) 
{
   using namespace std;
   // Declaring an object of type basic_string<char>
   string s1 ( "Tweedledee" );
   string s2 ( "Tweedledum" );
   cout << "Before swapping string s1 and s2:" << endl;
   cout << "The basic_string s1 = " << s1 << "." << endl;
   cout << "The basic_string s2 = " << s2 << "." << endl;

   swap ( s1 , s2 );
   cout << "\nAfter swapping string s1 and s2:" << endl;
   cout << "The basic_string s1 = " << s1 << "." << endl;
   cout << "The basic_string s2 = " << s2 << "." << endl;
}
             

需求

標頭:<string>

命名空間: std