std::basic_string::swap
From Get docs
< Strings library | basic stringCpp/docs/latest/string/basic string/swap
std::basic_string::swap
|
(until C++17) | |
|
(since C++17) |
Exchanges the contents of the string with those of other. All iterators and references may be invalidated.
The behavior is undefined if Allocator does not propagate on swap and the allocators of *this and other are unequal.
|
(since C++11) |
Parameters
| other | - | string to exchange the contents with |
Return value
(none).
Exceptions
|
(since C++17) |
Example
#include <string>
#include <iostream>
int main()
{
std::string a = "AAA";
std::string b = "BBB";
std::cout << "before swap" << '\n';
std::cout << "a: " << a << '\n';
std::cout << "b: " << b << '\n';
a.swap(b);
std::cout << "after swap" << '\n';
std::cout << "a: " << a << '\n';
std::cout << "b: " << b << '\n';
}
Output:
before swap
a: AAA
b: BBB
after swap
a: BBB
b: AAA
Complexity
Constant.
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/string/basic_string/swap