C++ 算法 中的 iter_swap函数

首页 / C++入门教程 / C++ 算法 中的 iter_swap函数

C++算法 iter_swap()交换两个迭代器a和b指向的元素。

iter_swap - 语法

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

iter_swap - 参数

a :要交换其值的正向迭代器之一。

b :要交换其值的正向迭代器的第二个。

iter_swap - 返回值

没有

iter_swap - 例子1

让我们看一个简单的示例来演示iter_swap()的用法:

#include <algorithm>
#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

int main() {
  vector<int> v1 = { 1, 2, 3 };
  vector<int> v2 = { 4, 5, 6 };

  iter_swap(v1.begin()+1, v2.begin()+2);

  cout << "v1: ";
  copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, ", "));
  cout << endl;

  cout << "v2: ";
  copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, ", "));
  cout << endl;
  
  return 0;
}

输出:

v1: 1, 6, 3, 
v2: 4, 5, 2, 

iter_swap - 例子2

让我们看另一个简单的例子:

#include <iostream> 
#include <algorithm> 
#include <vector> 

using namespace std; 

int main() 
{ 
   //Declaring first vector 
    vector<int> v1; 
    int i; 
  
    for (i = 0; i < 10; ++i) { 
        v1.push_back(i); 
    } 
   //v1 contains 0 1 2 3 4 5 6 7 8 9 
  
    vector<int>::iterator i1, i2; 
  
    i1 = v1.begin(); 
    i2 = v1.end() - 1; 
  
   //Performing swap between first and last element 
   //of vector 
    std::iter_swap(i1, i2); 
  
   //Displaying v1 after swapping 
    for (i = 0; i < 10; ++i) { 
        cout << v1[i] << " "; 
    } 
  
    return 0; 
}

输出:

9 1 2 3 4 5 6 7 8 0  

在上面的示例中,我们在两个迭代器的帮助下交换了v1中的元素,其中一个指向v1的开头,另一个指向v1的结尾。

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-algorithm-iter-swap-function.html

来源:LearnFk无涯教程网

iter_swap - 例子3

让我们看另一个简单的示例,将向量的奇数位交换为奇数步长的数组:

#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std; 
 
void print(int v)
{
    cout << v << "   ";
}
 
int main()
{
    int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    vector <int> v(10);
 
    cout << "Vector : ";
    for_each(v.begin(), v.end(), print);
    cout << endl;
    
    cout << "Array  : ";
    for_each(a, a + 10, print);
    cout << endl;
    
    for (int i = 1; i < 10; i+=2)
        iter_swap(v.begin() + i, a + i);
    cout << "\nSwapping odd places of vector with odd places of array"<< endl<<endl;
    
    cout << "Vector : ";
    for_each(v.begin(), v.end(), print);
    cout << endl;
    
    cout << "Array  : ";
    for_each(a, a + 10, print);
    cout << endl;
    
    return 0;
}

输出:

Vector : 0   0   0   0   0   0   0   0   0   0   
Array  : 1   2   3   4   5   6   7   8   9   10   

Swapping odd places of vector with odd places of array

Vector : 0   2   0   4   0   6   0   8   0   10   
Array  : 1   0   3   0   5   0   7   0   9   0  

iter_swap - 例子4

让我们看另一个简单的例子:

#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
int main()
{
  int a1[] = {1, 2, 3, 4, 5};
  vector<int> v1(a1, a1+5);
  cout <<"Here are the contents of v1:\n";
  for (vector<int>::size_type i=0; i<v1.size(); i++)
    cout <<v1.at(i)<<" ";

  int a2[] = {2, 4, 6, 8, 10};
  vector<int> v2(a2, a2+5);
  cout <<"\nHere are the contents of v2:\n";
  for (vector<int>::size_type i=0; i<v2.size(); i++)
    cout <<v2.at(i)<<" ";
 
  cout <<"\n\nFirst we swap the end values in v1.";
  iter_swap(v1.begin(), v1.end()-1);
  cout <<"\nHere are the contents of the revised v1:\n";
  for (vector<int>::size_type i=0; i<v1.size(); i++)
    cout <<v1.at(i)<<" ";
 
  cout <<"\n\nThen we swap the middle values in v1 and v2.";
  iter_swap(v1.begin()+2, v2.begin()+2);
  cout <<"\nHere are the contents of the revised v1:\n";
  for (vector<int>::size_type i=0; i<v1.size(); i++)
    cout <<v1.at(i)<<" ";
 
  cout <<"\nHere are the contents of the revised v2:\n";
  for (vector<int>::size_type i=0; i<v2.size(); i++)
    cout <<v2.at(i)<<" ";
 
  return 0;
}

输出:

Here are the contents of v1:
1 2 3 4 5 
Here are the contents of v2:
2 4 6 8 10 

First we swap the end values in v1.
Here are the contents of the revised v1:
5 2 3 4 1 

Then we swap the middle values in v1 and v2.
Here are the contents of the revised v1:
5 2 6 4 1 
Here are the contents of the revised v2:
2 4 3 8 10  

上面的示例说明了使用iter_swap()算法交换由两个不同的迭代器指向的整数值,这些迭代器可能指向同一整数矢量,或者指向两个不同的整数矢量。

iter_swap - 例子5

让我们看另一个示例来说明iter_swap()的用法:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


// return the next Fibonacci number in the
// Fibonacci series.
int Fibonacci(void)
{
    static int r;
    static int f1 = 0;
    static int f2 = 1;
    r = f1 + f2 ;
    f1 = f2 ;
    f2 = r ;
    return f1 ;
}

int main()
{
    const int VECTOR_SIZE = 8 ;

   //Define a template class vector of integers
    typedef vector<int > IntVector ;

    //Define an iterator for template class vector of integer
    typedef IntVector::iterator IntVectorIt ;

    IntVector Numbers(VECTOR_SIZE) ;   //vector containing numbers

    IntVectorIt start, end, it ;

    start = Numbers.begin() ;  //location of first
                               //element of Numbers

    end = Numbers.end() ;      //one past the location
                               //last element of Numbers

   //fill the range [first, last +1) with a series of
   //Fibonacci numbers using the Fibonacci function
    generate(start, end, Fibonacci) ;

    cout << "Before calling iter_swap" << endl ;

   //print content of Numbers
    cout << "Numbers { " ;
    for(it = start; it != end; it++)
        cout << *it << " " ;
    cout << " }\n" << endl ;

   //swap the first and last elements of the
   //sequence using iter_swap
    iter_swap(start, end - 1) ;

    cout << "After calling iter_swap" << endl ;

   //print content of Numbers
    cout << "Numbers { " ;
    for(it = start; it != end; it++)
        cout << *it << " " ;
    cout << " }\n" << endl ;
    
    return 0;

}

输出:

Before calling iter_swap
Numbers { 1 1 2 3 5 8 13 21  }

After calling iter_swap
Numbers { 21 1 2 3 5 8 13 1  }

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

白话法律42讲 -〔周甲徳〕

玩转Spring全家桶 -〔丁雪丰〕

Linux实战技能100讲 -〔尹会生〕

DevOps实战笔记 -〔石雪峰〕

DDD实战课 -〔欧创新〕

Selenium自动化测试实战 -〔郭宏志〕

大厂广告产品心法 -〔郭谊〕

Serverless进阶实战课 -〔静远〕

手把手带你写一个 MiniTomcat -〔郭屹〕

好记忆不如烂笔头。留下您的足迹吧 :)