C++ STL算法之:copy

 

C++ STL算法:copy


目录(?)[+]

       前面十二个算法所展现的都属于非变易算法(Non-mutating algorithms)系列,现在我们来看看变易算法。所谓变易算法(Mutating algorithms)就是一组能够修改容器元素数据的模板函数,可进行序列数据的复制,变换等。

       我们现在来看看第一个变易算法:元素复制算法copy。该算法主要用于容器之间元素的拷贝,即将迭代器区间[first,last)的元素复制到由复制目标result给定的区间[result,result+(last-first))中。下面我们来看看它的函数原型:

函数原形:

[cpp] view plain copy
  1. template<class InputIterator, class OutputIterator>  
  2.    OutputIterator copy(  
  3.       InputIterator _First,   
  4.       InputIterator _Last,   
  5.       OutputIterator _DestBeg  
  6.    );  

参数

_First, _Last
指出被复制的元素的区间范围[ _First,_Last).
_DestBeg 
指出复制到的目标区间起始位置

返回值

返回一个迭代器,指出已被复制元素区间的最后一个位置

程序示例:

首先我们来一个简单的示例,定义一个简单的整形数组myints,将其所有元素复制到容器myvector中,并将数组向左移动一位。

[cpp] view plain copy
  1. /******************************************************************* 
  2.  * Copyright (C) Jerry Jiang 
  3.  *                
  4.  * File Name   : copy01.cpp 
  5.  * Author      : Jerry Jiang 
  6.  * Create Time : 2012-3-20 22:44:28 
  7.  * Mail        : jbiaojerry@gmail.com 
  8.  * Blog        : http://blog.csdn.net/jerryjbiao  
  9.  *                
  10.  * Description :  简单的程序诠释C++ STL算法系列之十三                  
  11.  *                变易算法 : 元素复制copy   
  12.  *                
  13.  ******************************************************************/  
  14.   
  15. #include <iostream>  
  16. #include <algorithm>  
  17. #include <vector>  
  18.   
  19. using namespace std;  
  20.   
  21. int main ()   
  22. {  
  23.     int myints[] = {10, 20, 30, 40, 50, 60, 70};  
  24.     vector<int> myvector;  
  25.     vector<int>::iterator it;  
  26.       
  27.     myvector.resize(7);   // 为容器myvector分配空间  
  28.       
  29.     //copy用法一:  
  30.     //将数组myints中的七个元素复制到myvector容器中  
  31.     copy ( myints, myints+7, myvector.begin() );  
  32.       
  33.     cout << "myvector contains: ";  
  34.     for ( it = myvector.begin();  it != myvector.end();  ++it )  
  35.     {  
  36.         cout << " " << *it;  
  37.     }  
  38.     cout << endl;  
  39.   
  40.     //copy用法二:  
  41.     //将数组myints中的元素向左移动一位  
  42.     copy(myints + 1, myints + 7, myints);  
  43.   
  44.     cout << "myints contains: ";  
  45.     for ( size_t i = 0; i < 7; ++i )  
  46.     {  
  47.         cout << " " << myints[i];  
  48.     }  
  49.     cout << endl;  
  50.   
  51.     return 0;  
  52. }  


从上例中我们看出copy算法可以很简单地将一个容器里面的元素复制至另一个目标容器中,上例中代码特别要注意一点就是myvector.resize(7);这行代码,在这里一定要先为vector分配空间,否则程序会崩,这是初学者经常犯的一个错误。其实copy函数最大的威力是结合标准输入输出迭代器的时候,我们通过下面这个示例就可以看出它的威力了。

[cpp] view plain copy
  1. /******************************************************************* 
  2.  * Copyright (C) Jerry Jiang 
  3.  *                
  4.  * File Name   : copy2.cpp 
  5.  * Author      : Jerry Jiang 
  6.  * Create Time : 2012-3-20 23:25:29 
  7.  * Mail        : jbiaojerry@gmail.com 
  8.  * Blog        : http://blog.csdn.net/jerryjbiao  
  9.  *                
  10.  * Description :  简单的程序诠释C++ STL算法系列之十三                  
  11.  *                变易算法 : 元素复制copy   
  12.  *                
  13.  ******************************************************************/  
  14.   
  15. #include <iostream>  
  16. #include <algorithm>  
  17. #include <vector>  
  18. #include <iterator>  
  19. #include <string>  
  20.   
  21. using namespace std;  
  22.   
  23. int main ()   
  24. {  
  25.      typedef vector<int> IntVector;  
  26.      typedef istream_iterator<int> IstreamItr;  
  27.      typedef ostream_iterator<int> OstreamItr;  
  28.      typedef back_insert_iterator< IntVector > BackInsItr;  
  29.    
  30.      IntVector myvector;  
  31.   
  32.      // 从标准输入设备读入整数  
  33.      // 直到输入的是非整型数据为止 请输入整数序列,按任意非数字键并回车结束输入  
  34.      cout << "Please input element:" << endl;  
  35.      copy(IstreamItr(cin), IstreamItr(), BackInsItr(myvector));  
  36.   
  37.      //输出容器里的所有元素,元素之间用空格隔开  
  38.      cout << "Output : " << endl;  
  39.      copy(myvector.begin(), myvector.end(), OstreamItr(cout, " "));   
  40.      cout << endl;  
  41.   
  42.     return 0;  
  43. }  
转自:   http://blog.csdn.net/jerryjbiao/article/details/7376088
posted @ 2016-04-26 11:16  Free_Open  阅读(400)  评论(0编辑  收藏  举报