c++ STL之 copy(权哥)

来源:互联网 发布:舞美设计用什么软件 编辑:程序博客网 时间:2024/06/09 15:20

这个demo演示STL容器间数据的拷贝方法:

#include <algorithm>#include <iostream>using namespace std;#include <vector>#include "print.h"#include <deque>/*模拟copy*/template < class Iter, class Pos >void co(Iter ib, Iter ie, Pos p){while(ib!=ie) *p++=*ib++;}/*模拟copy_backward*/template < class Iter, class Pos >void cb(Iter ib, Iter ie, Pos p){while(ib!=ie) *--p=*--ie;}int main(){int a[5]={3,9,2,6,8};int b[8]={0};vector<int> vi(a,a+5);sort(vi.begin(),vi.end());print(vi.begin(),vi.end());print(a,a+5,',');copy(vi.begin(),vi.end(),a);print(a,a+5,',');co(vi.begin(),vi.end(),b);print(b,b+8);copy_backward(b,b+5,b+8);//反向拷贝,此处出现了数据覆盖print(b,b+8);bool func(int);deque<int> v;co(a,a+5,back_inserter(v));//后插入print(v.begin(), v.end());co(a,a+5,front_inserter(v));//前插入print(v.begin(), v.end());v.clear();remove_copy_if(a,a+5,back_inserter(v),func);//条件拷贝:func返回bool 1print(v.begin(), v.end());}bool func(int n){return n&1;}/*输出:2 3 6 8 9 3,9,2,6,8,2,3,6,8,9,2 3 6 8 9 0 0 0 2 3 6 2 3 6 8 9 2 3 6 8 9 9 8 6 3 2 2 3 6 8 9 2 6 8 */


0 0
原创粉丝点击