public member function
<string>
void swap ( string& str );
Swap contents with another string
Swaps the contents of the string with those of
string object
str, such that after the call to this member function, the contents of this string are those which were in
str before the call, and the contents of
str are those which were in this string.
Notice that a global function with the same name,
swap, exists with the same behavior, and which acts as a specialization for strings of the algorithm function with the same name.
Parameters
- str
- a string object to swap its contents with those of the object.
Return value
none
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
// swap strings
#include <iostream>
#include <string>
using namespace std;
main ()
{
string buyer ("money");
string seller ("goods");
cout << "Before swap, buyer has " << buyer;
cout << " and seller has " << seller << endl;
seller.swap (buyer);
cout << " After swap, buyer has " << buyer;
cout << " and seller has " << seller << endl;
return 0;
}
|
Output:
Before swap, buyer has money and seller has goods
After swap, buyer has goods and seller has money |
Basic template member declaration
( basic_string<charT,traits,Allocator> )
|
void swap ( basic_string& str );
|
See also
- swap
- Swap contents of two strings (function
)
- string::replace
- Replace part of string (public member function)
- string::assign
- Assign content to string (public member function)