本节描述的算法会变动区间内的元素内容。有两种方法可以变动元素内容:

1.运用迭代器遍历序列的过程中,直接加以变动

2.将元素从源区间赋值到目标区间的过程中加以变动

 

复制(copy)元素

OutputIterator

copy(InputIterator sourceBeg,

        InputIterator sourceEnd,

        OutputIterator destBeg)

BiderectionalIterator

copy_backward(BidirectionalIterator sourceBeg,

                          BidirectionalIterator sourceEnd,

                          BidirectionalIterator destEnd)

1.这两个算法都将源区间[sourceBeg,sourceEnd)中的所有元素赋值到以destBeg为起点或以destEnd为终点的目标区间去

2.返回目标区间内最后一个被赋值元素的下一位置,也就是第一个违背覆盖的元素的位置

3.destBeg或destEnd不可处于[sourceBeg,sourceEnd)区间内

 

下面的例子展示copy()的一些简单用法

 1 #include <iterator>
 2 #include "algostuff.hpp"
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     vector<int> coll1;
 8     list<int> coll2;
 9     INSERT_ELEMENTS(coll1,1,9);
10     copy(coll1.begin(),coll1.end(),back_inserter(coll2));
11     copy(coll2.begin(),coll2.end(),ostream_iterator<int>(cout," "));
12     cout<<endl;
13     copy(coll1.rbegin(),coll1.rend(),coll2.begin());
14     copy(coll2.begin(),coll2.end(),ostream_iterator<int>(cout," "));
15     cout<<endl;
16 }
View Code

相关文章:

  • 2022-02-01
  • 2021-07-02
  • 2022-02-16
  • 2021-10-31
  • 2022-12-23
  • 2021-12-14
  • 2021-12-18
  • 2021-06-01
猜你喜欢
  • 2021-10-28
  • 2021-06-07
  • 2021-12-03
  • 2021-08-14
  • 2021-10-15
  • 2022-12-23
  • 2021-12-03
相关资源
相似解决方案