public member function
<string>
std::basic_string::swap
void swap (basic_string& str);
Swap string values
After the call to this member function, the value of this object is the value str had before the call, and the value of str is the value this object had before the call.
Notice that a non-member function exists with the same name, swap, overloading that algorithm with an optimization that behaves like this member function.
Parameters
- str
- Another basic_string object of the same type (i.e., instantiated with the same template parameters, charT, traits and Alloc), whose value is swapped with that of this basic_string.
Return value
noneExample
|
|
Output:
Before the swap, buyer has money and seller has goods After the swap, buyer has goods and seller has money
Complexity
Constant.Iterator validity
Any iterators, pointers and references related to this object and to str may be invalidated.Data races
Both the object and str are modified.Exception safety
If the allocators in both strings compare equal, or if their allocator traits indicate that the allocators shall propagate, the function never throws exceptions (no-throw guarantee).Otherwise, it causes undefined behavior.
See also
- swap (basic_string)
- Exchanges the values of two strings (function template)
- swap_ranges
- Exchange values of two ranges (function template)