在线文档教程
C++
字符串 | Strings

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::propagate_on_container_swap::value || std::allocator_traits::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。

http://en.cppreference.com/w/cpp/string/basic[医]字符串/交换