function template
<string>
std::swap (basic_string)
template <class charT, class traits, class Alloc> void swap (basic_string<charT,traits,Alloc>& x, basic_string<charT,traits,Alloc>& y);
Exchanges the values of two strings
This is an overload of the generic algorithm swap that improves its performance by mutually transferring ownership over their internal data to the other object (i.e., the strings exchange references to their data, without actually copying the characters): It behaves as if
x.swap(y) was called.Parameters
- x,y
- basic_string objects of the same type (i.e., having both the same template parameters, charT, traits and Alloc).
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 both x and y may be invalidated.Data races
Both objects, x and y, 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
- basic_string::swap
- Swap string values (public member function)
- swap
- Exchange values of two objects (function template)
- swap_ranges
- Exchange values of two ranges (function template)