basic_string::swap

交换两个字符串的内容。

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

参数

  • _Str
    元素将交换与在目标字符串的源字符串。

备注

如果 Exchange 的字符串具有相同的程序分配对象,swap 成员函数:

  • 在常数的时间发生。

  • 不引发异常。

  • 无效指定两个字符串的元素的引用、指针或迭代器。

否则,它将执行大量的元素赋值,然后构造函数调用而与元素数目在两个控件的顺序。

示例

// 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 类