vector-erase

1人阅读 评论(0) 收藏 举报
分类:
////////////////////////////////////////
//      2018/04/16 15:34:21
//      vector-erase

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

using namespace std;

int main(){
    vector<int> v(10);

    vector<int>::iterator it;
    for (int i = 0; i < 10; i++){
        v[i] = i;
    }
    copy(v.begin(),v.end(), ostream_iterator<int>(cout, " "));
    cout << endl;

    // remove first element
    it = v.begin();
    v.erase(it);
    copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
    cout << endl;

    // remove third element
    it = v.begin()+2;
    v.erase(it);
    copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
    cout << endl;

    //remove 2 elements from begin to v
    it = v.begin();
    v.erase(it, it + 2);
    copy(v.begin(),v.end(), ostream_iterator<int>(cout, " "));
    cout << endl;

    return 0;
}

/*
OUTPUT:
    0 1 2 3 4 5 6 7 8 9
    1 2 3 4 5 6 7 8 9
    1 2 4 5 6 7 8 9
    4 5 6 7 8 9
*/ 
查看评论
    个人资料
    持之以恒
    等级:
    访问量: 4万+
    积分: 1714
    排名: 2万+
    xxxxx