名前空間
変種
操作

std::basic_string<CharT,Traits,Allocator>::swap

提供: cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
 
void swap( basic_string& other );
(C++17未満)
void swap( basic_string& other ) noexcept(/* see below */);
(C++17以上)
(C++20未満)
constexpr void swap( basic_string& other ) noexcept(/* see below */);
(C++20以上)

文字列の内容を other の内容と交換します。 すべてのイテレータおよび参照は無効化されるかもしれません。

Allocator が swap 時に伝播せず、 *thisother のアロケータが等しくない場合、動作は未定義です。

(C++11以上)

目次

[編集] 引数

other - 内容を交換する文字列

[編集] 戻り値

(なし)

例外

noexcept 指定:  
noexcept(std::allocator_traits<Allocator>::propagate_on_container_swap::value
|| std::allocator_traits<Allocator>::is_always_equal::value)
(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

[編集] 計算量

一定。