GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::basic_string::swap - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. string
  3. basic_string
  4. swap

最終更新日時: 2026年08月27日 14時29分14秒 (JST)
Akira Takahashi が更新

履歴 編集

function
<string>

std::basic_string::swap

void swap(basic_string& str);                                // (1) C++98
void swap(basic_string& str) noexcept
  (allocator_traits<Allocator>::propagate_on_container_swap::value
   || allocator_traits<Allocator>::is_always_equal::value);  // (1) C++17
constexpr void swap(basic_string& str) noexcept
  (allocator_traits<Allocator>::propagate_on_container_swap::value
   || allocator_traits<Allocator>::is_always_equal::value);  // (1) C++20

概要

他のbasic_stringオブジェクトとデータを入れ替える。

効果

*thisの内容をstrと交換する。

戻り値

なし

計算量

定数時間

備考

  • swapされるコンテナの要素を指す参照、ポインタ、イテレータを無効化しない。
    • ただし、end()が指すイテレータはどの要素も指していないが、無効になることがある。

例

#include <iostream>
#include <string>

int main()
{
  std::string a = "hello";
  std::string b = "world";

  a.swap(b);

  std::cout << a << std::endl;
  std::cout << b << std::endl;
}

出力

world
hello

参照

  • N4258 Cleaning-up noexcept in the Library, Rev 3
    • noexcept 追加の経緯となる提案文書
  • P0980R1 Making std::string constexpr
  • LWG Issue 765. More on iterator validity
    • C++11で、swap()が参照・ポインタ・イテレータを無効化しないという要件に対して、「end()が返すイテレータはどの要素も指していないため、無効化されることがある」という注記が追加された
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.