• vector-insert


    ////////////////////////////////////////
    //      2018/04/16 19:48:47
    //      vector-insert
    #include <iostream>
    #include <vector>
    #include <iterator>
    #include <algorithm>
    #include <numeric>
    
    using namespace std;
    
    template<class T>
    class Print{
    public:
        void operator()(T& t){
            cout << t << " ";
        }
    };
    
    // ===========================
    
    int main(){
        int ary[5];
        fill(ary, ary + 5, 1);
    
        vector<int> v(5);
        vector<int>::iterator it;
        Print<int> print;
        //copy(ary, ary + 5, back_inserter(v));
        iota(v.begin(),v.end(),1);
    
    
        cout << "Vector v :" << endl;
        for_each(v.begin(),v.end(), print);
        cout << endl;
    
        it = v.begin();
        // insert value "5" at the position "it"
        cout << "v.insert(it,10)" << endl;
        v.insert(it, 10);
        for_each(v.begin(), v.end(), print);
        cout << endl;
    
        // insert range ary+2 - ary+5 at the position "it"
        it = v.begin()+5;
        cout << "v.insert(it,ary+2,ary+5)" << endl;
        v.insert(it, ary + 2, ary + 5);
        for_each(v.begin(), v.end(), print);
        cout << endl;
    
        // insert 2 value of "20" at the position "it"
        it = v.end() - 2;
        cout << "v.insert(it,2,20)";
        v.insert(it, 2, 20);
        for_each(v.begin(),v.end(),print);
        cout << endl;
        return 0;
    }
    
    /*
    OUTPUT:
        Vector v :
        1 2 3 4 5
        v.insert(it,10)
        10 1 2 3 4 5
        v.insert(it,ary+2,ary+5)
        10 1 2 3 4 1 1 1 5
        v.insert(it,2,20)10 1 2 3 4 1 1 20 20 1 5
    */
    
  • 相关阅读:
    php RSA 简单实现
    redis 常用操作命令
    ajax短轮询+php与服务器交互制作简易即时聊天网站
    PHP解决网站大数据大流量与高并发
    Memcache所有方法及参数详解
    apache 与 nginx 详解
    apache 与 nginx的区别
    Redis,Memcache的区别和具体应用场景
    Memcache Redis 与Mogodb优缺点
    MySQL 存储
  • 原文地址:https://www.cnblogs.com/laohaozi/p/12538039.html
Copyright © 2020-2023  润新知