function
std::swap
<string>
void swap ( string& lhs, string& rhs);
Swap contents of two strings
Swaps the contents of the
string objects
lhs and
rhs, such that after the call to this function, the contents of
rhs are those which were in
lhs before the call, and the contents of
lhs are those which were in
rhs.
Notice that
string objects implement a member function also called
swap; In fact, this global function effectively calls:
lhs.swap(rhs);
Parameters
- lhs
- a string object to be swapped.
- rhs
- the other string object to be swapped.
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;
swap (buyer,seller);
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 declaration
1 2 3
|
template<class charT, class traits, class Allocator>
void swap ( basic_string<charT,traits,Allocator>& lhs,
basic_string<charT,traits,Allocator>& rhs);
|
See also
- string::swap
- Swap contents with another string (public member function)
- string::replace
- Replace part of string (public member function)
- string::assign
- Assign content to string (public member function)