basic_string::swap

2 つの文字列の内容を交換します。

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

パラメーター

  • _Str
    要素のコピー先の文字列との間で交換される元の文字列。

解説

交換される文字列に同じアロケーター オブジェクトがある場合は、swap のメンバー関数:

  • 定数時間で発生します。

  • 例外をスローしません。

  • 2 桁の文字列要素を指定する参照、ポインター、または反復子は無効になりません。

それ以外の場合、2 つの被制御シーケンス内の要素数に比例した回数、要素の割り当てとコンストラクター呼び出しが実行されます。

使用例

// 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;
}
  

必要条件

ヘッダー: の <文字列>

名前空間: std

参照

関連項目

basic_string クラス