C++ vector增删改查遍历排序去重

int main()
{
    vector<int> test;
    //插入
    test.push_back(0);
    test.insert(test.begin(),1);//在迭代器之前插入
    test.insert(test.end(), 3, 2);
    //遍历
    for (auto i : test)
        cout << i << " ";
    cout << endl;
    //删除
    test.erase(test.end()-1, test.end());
    //查找
    if (find(test.begin(), test.end(), 1) != test.end())
        cout << "find it" << endl;
    //排序
    sort(test.begin(), test.end());
    //去重
    test.erase(unique(test.begin(), test.end()), test.end());
    //
    test.reserve(10);
    test.resize(7);
    return 0;
}

 

posted @ 2018-08-24 10:59  aote369  阅读(1156)  评论(0编辑  收藏  举报