swap (<string>)
交换一些两个字符串的字符。
template<class Traits, class Allocator>
void swap(
basic_string<CharType, Traits, Allocator>& _Left,
basic_string<CharType, Traits, Allocator>& _Right
);
参数
_Left
组件将交换与其他字符串的字符串。_Right
组件将交换与第一个字符串的另一个字符串。
备注
模板函数执行专用的成员函数_Left。交换(_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