std::swap(std::basic_string)

来自cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
 
在标头 <string> 定义
template<class CharT, class Traits, class Alloc >

void swap(std::basic_string<CharT, Traits, Alloc>& lhs,

           std::basic_string<CharT, Traits, Alloc>& rhs );
(C++17 前)
template<class CharT, class Traits, class Alloc >

void swap(std::basic_string<CharT, Traits, Alloc>& lhs,

           std::basic_string<CharT, Traits, Alloc>& rhs )noexcept(/* 见下文 */);
(C++17 起)
(C++20 起为 constexpr)

std::basic_string 特化 std::swap 算法。交换 lhsrhs 的内容。等价于 lhs.swap(rhs)

目录

[编辑]参数

lhs, rhs - 要交换内容的字符串

[编辑]返回值

(无)

[编辑]复杂度

常数。

异常

noexcept 说明:  
noexcept(noexcept(lhs.swap(rhs)))
(C++17 起)

[编辑]示例

#include <iostream>#include <string>   int main(){std::string a ="AAA";std::string b ="BBBB";   std::cout<<"交换前:\n""a = "<< a <<"\n""b = "<< b <<"\n\n";   std::swap(a, b);   std::cout<<"交换后:\n""a = "<< a <<"\n""b = "<< b <<'\n';}

输出:

交换前: a = AAA b = BBBB   交换后: a = BBBB b = AAA

[编辑]缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 出版时的行为 正确行为
LWG 2064 C++11 非成员 swap 为 noexcept 并与成员 swap 不一致 移除 noexcept

[编辑]参阅

交换内容
(公开成员函数)[编辑]
close