copy : Assigns the values of elements from a source range to a destination range, iterating through the source sequence of elements and assigning them new positions in a forward direction.将容器中的元素从一个区间复制到另一个区间。copy(first_source, Last_source, dest) 进行的是前向处理。
Neal: 可以往first_source, Last_source 之间的空间拷贝。
Code:
// Vector_Learning.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <vector> //#include <list> #include <iostream> //#include <algorithm> #include <numeric> //#include <assert.h> //#include <map> #include <string> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int ia[] = { 0, 1, 1, 3, 5, 8, 13}; vector<int> vec(ia, ia + 7); ostream_iterator<int> ofile(cout, " "); cout<<"original element sequence:/n"; copy(vec.begin(), vec.end(), ofile); cout<<'/n'; copy(ia + 1, ia + 7, ia); cout<<"shifting array sequence left by 1:/n"; copy(ia, ia + 7, ofile); cout<<'/n'; copy(vec.begin() + 2, vec.end(), vec.begin()); cout<<"shifting vector sequence left by 2:/n"; copy(vec.begin(), vec.end(), ofile); cout<<'/n'; system("pause"); return 0; }
output:
original element sequence: 0 1 1 3 5 8 13 shifting array sequence left by 1: 1 1 3 5 8 13 13 shifting vector sequence left by 2: 1 3 5 8 13 8 13 请按任意键继续. . . 请按任意键继续. . .