目录

swap

描述 (Description)

它通过str的内容交换容器的内容,str是另一个字符串对象。 长度可能不同。

声明 (Declaration)

以下是std :: string :: swap的声明。

void swap (string& str);

C++11

void swap (string& str);

C++14

void swap (string& str);

参数 (Parameters)

str - 它是一个字符串对象。

返回值 (Return Value)

没有

异常 (Exceptions)

如果抛出异常,则字符串中没有变化。

例子 (Example)

在下面的示例中为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
↑回到顶部↑
WIKI教程 @2018