namespace std {
template <class CharT, class Traits, class Allocator>
void swap(basic_string<CharT, Traits, Allocator>& x,
basic_string<CharT, Traits, Allocator>& y); // (1) C++03
template <class CharT, class Traits, class Allocator>
void swap(basic_string<CharT, Traits, Allocator>& x,
basic_string<CharT, Traits, Allocator>& y)
noexcept(noexcept(lhs.swap(rhs))); // (1) C++17
template <class CharT, class Traits, class Allocator>
constexpr
void swap(basic_string<CharT, Traits, Allocator>& x,
basic_string<CharT, Traits, Allocator>& y)
noexcept(noexcept(lhs.swap(rhs))); // (1) C++20
}
概要
2つのbasic_string
オブジェクトを入れ替える
効果
x.swap(y)
戻り値
なし
例
#include <iostream>
#include <string>
int main()
{
std::string a = "hello";
std::string b = "world";
std::swap(a, b);
std::cout << a << std::endl;
std::cout << b << std::endl;
}
14
#include <iostream>
#include <string>
int main()
{
std::string a = "hello";
std::string b = "world";
std::swap(a, 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