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
*/ 
原文地址:https://www.cnblogs.com/laohaozi/p/12537960.html