共用方式為


basic_string::swap

交換兩個字串的內容。

void swap(
    basic_string<CharType, Traits, Allocator>& _Str
);

參數

  • _Str
    要將其項目切換與目的資料的來源字串。

備註

如果交換字串具有相同配置器物件, swap 成員函式:

  • 在常數時間發生。

  • 不會擲回例外狀況。

  • 不無法將兩個字串的元素的參考、指標或 Iterator。

否則,它會執行許多工作項目,而且建構函式呼叫比例與項目數目兩個受控制序列的。

範例

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

int main( ) 
{
   using namespace std;

   // Declaring an objects 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;

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

需求

標頭:<string>

命名空間: std

請參閱

參考

basic_string 類別