C++ String 库 - swap
描述
它通过另一个字符串对象str的内容来交换容器的内容。 长度可能不同。
声明
以下是 std::string::swap 的声明。
void swap (string& str);
C++11
void swap (string& str);
C++14
void swap (string& str);
参数
str − 它是一个字符串对象。
返回值
none
异常
如果抛出异常,则字符串没有变化。
示例
在下面的 std::string::swap 示例中。
#include <iostream>
#include <string>
main () {
std::string buyer ("money");
std::string seller ("goods");
std::cout << "Before the swap, buyer has " << buyer;
std::cout << " and seller has " << seller << '\n';
seller.swap (buyer);
std::cout << " After the swap, buyer has " << buyer;
std::cout << " and seller has " << seller << '\n';
return 0;
}
示例输出应该是这样的 −
Before the swap, buyer has money and seller has goods After the swap, buyer has goods and seller has money

