std::basic_string::swap
性病:基本[医]字符串:交换
void swap( basic_string& other | | (until C++17) |
---|---|---|
void swap( basic_string& other ) noexcept(/* see below */ | | (since C++17) |
将字符串的内容与other
.所有迭代器和引用可能无效。
参数
other | - | string to exchange the contents with |
---|
返回值
%280%29
Exceptions noexcept specification: noexcept(std::allocator_traits | (since C++17) |
---|
例
二次
#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';
}
二次
产出:
二次
before swap
a: AAA
b: BBB
after swap
a: BBB
b: AAA
二次
复杂性
常量。
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。