首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

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)

二次

代码语言:javascript
复制
#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';
}

二次

产出:

二次

代码语言:javascript
复制
before swap
a: AAA
b: BBB
after swap
a: BBB
b: AAA

二次

复杂性

常量。

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券