目录

swap

描述 (Description)

它交换a和b的值。

声明 (Declaration)

以下是std :: swap函数的声明。

template <class T> void swap (T& a, T& b);

C++11

template <class T> void swap (T& a, T& b)
   noexcept (is_nothrow_move_constructible<T>::value && is_nothrow_move_assignable<T>::value);

参数 (Parameters)

a, b - 这是两个对象。

返回值 (Return Value)

没有

异常 (Exceptions)

Basic guarantee - 如果T型的构造或分配抛出。

数据竞争 (Data races)

a和b都被修改。

例子 (Example)

在下面的例子中解释了std :: swap函数。

#include <iostream>
#include <utility>
int main () {
   int foo[4];
   int bar[] = {100,200,300,400};
   std::swap(foo,bar);
   std::cout << "foo contains:";
   for (int i: foo) std::cout << ' ' << i;
   std::cout << '\n';
   return 0;
}

让我们编译并运行上面的程序,这将产生以下结果 -

foo contains: 100 200 300 400
↑回到顶部↑
WIKI教程 @2018