swap(C++ STL <字符串>)

交换两个字符串的字符数组。

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