1.功能:将某容器中的指定元素拷贝到另一个容器中
2.函数原型
- copy(iterator beg, iterator end, iterator dest)
- beg 开始迭代器
- end 结束迭代器
- dest 目标起始迭代器
#include<iostream> #include<vector> #include<algorithm> #include<ctime> using namespace std; void myprint(int val) { cout << val << " "; } void test1() { vector<int> v; for (int i = 0; i < 10; i++) { v.push_back(i); } //函数拷贝 vector<int> vv; vv.resize(v.size()); copy(v.begin(), v.end(), vv.begin()); for_each(vv.begin(), vv.end(), myprint); //遍历容器vv 0 1 2 3 4 5 6 7 8 9 cout << endl; //赋值拷贝 vector<int> vvv(v); for_each(vvv.begin(), vvv.end(), myprint); //遍历容器vvv 0 1 2 3 4 5 6 7 8 9 cout << endl; } int main() { test1(); return 0; }