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<Allocator>::propagate_on_container_swap::value || std::allocator_traits<Allocator>::is_always_equal::value) | (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。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com