目录

algorithm::iter_swap()

描述 (Description)

C ++函数std::algorithm::iter_swap()交换两个迭代器指向的对象的值。 它使用函数交换(不合格)来交换元素。

声明 (Declaration)

以下是std :: algorithm :: iter_swap()函数形式std :: algorithm头的声明。

C++98

template <class ForwardIterator1, class ForwardIterator2>
void iter_swap (ForwardIterator1 a, ForwardIterator2 b);

参数 (Parameters)

  • a - 第一个转发迭代器对象。

  • b - 第二个前向迭代器对象。

返回值

没有

异常 (Exceptions)

如果交换函数抛出异常,则抛出异常。

请注意,无效参数会导致未定义的行为。

时间复杂

不变。

例子 (Example)

以下示例显示了std :: algorithm :: iter_swap()函数的用法。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(void) {
   vector<int> v1 = {1, 2, 3, 4, 5};
   vector<int> v2 = {10, 20, 30, 40, 50};
   iter_swap(v1.begin(), v2.begin());
   iter_swap(v1.begin() + 1, v2.begin() + 2);
   cout << "Vector v2 contains following elements" << endl;
   for (auto it = v2.begin(); it != v2.end(); ++it)
      cout << *it << endl;
   return 0;
}

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

Vector v2 contains following elements
1
20
2
40
50
↑回到顶部↑
WIKI教程 @2018