• list-insert


    ////////////////////////////////////////
    //      2018/04/25 21:45:11
    //      list-insert
    
    // insert elements into the list
    #include <iostream>
    #include <list>
    #include <algorithm>
    #include <numeric>
    
    using namespace std;
    
    template<class T>
    void print(list<T> l){
        list<T>::iterator it = l.begin();
    
        while (it != l.end()){
            cout << *(it++) << " ";
        }
        cout << endl;
    }
    
    //================================
    int main(){
        list<int> li1(10, 0);
        list<int> li2(5);
        list<int>::iterator it;
    
        iota(li2.begin(), li2.end(), 1);
        cout << "li1:";
        print<int>(li1);
        cout << "li2:";
        print<int>(li2);
    
        it = li1.begin();
        // value of 20 at the pos it
        li1.insert(++it,20);
        cout << "li1:";
        print<int>(li1);
    
        // two value of the 25 at the beginning
        li1.insert(li1.begin(),2,25);
        cout << "li1:";
        print<int>(li1);
    
        // contents of li2 at the end of the li1
        li1.insert(li1.end(),li2.begin(), li2.end());
        cout << "li1:";
        print<int>(li1);
    
        return 0;
    }
    
    /*
    OUTPUT:
        li1:0 0 0 0 0 0 0 0 0 0
        li2:1 2 3 4 5
        li1:0 20 0 0 0 0 0 0 0 0 0
        li1:25 25 0 20 0 0 0 0 0 0 0 0 0
        li1:25 25 0 20 0 0 0 0 0 0 0 0 0 1 2 3 4 5
    */ 
  • 相关阅读:
    python 安装与pip安装
    使用通配符来解决数据1和11、12/13/14的问题
    数据库中一行变多行,拆分数据
    15-哈希表 HashTable
    13-自平衡二分搜索树 AVLTree
    12-并查集 UnionFind
    11-字典树 Trie
    10-线段树 Segment Tree
    09-堆 Heap(最大堆)
    08-映射 Map
  • 原文地址:https://www.cnblogs.com/laohaozi/p/12537961.html
Copyright © 2020-2023  润新知