C++ list增删改查遍历反转排序

int main()
{
    list<int> test;
    //插入
    test.push_back(1);
    test.push_back(1);
    test.push_front(0);
    test.push_back(2);
    
    auto it = test.begin();
    for (int count = 0; it != test.end() && count < 2; it++, count++);
    test.insert(it, 3);//在迭代器之前插入

    //排序
    test.sort();
    //遍历
    for (auto &i : test)
        cout << i << endl;
    //删除
    for (auto it = test.begin(); it != test.end();){
        if (*it != 2)
            it++;
        else
            it=test.erase(it);//erase会销毁迭代器,并更新迭代器的值
    }

    test.push_back(INT_MAX);
    test.push_front(INT_MIN);
    test.pop_back();
    test.pop_front();

    test.erase(remove(test.begin(), test.end(), 1), test.end());//remove非成员函数,并不能真正删除,只是把要删除的数据扔到后面并返回尾迭代器的值
    //不支持随机访问
    //cout << test[0] << endl;
    //查找
    if (find(test.begin(), test.end(), 3) != test.end())
        cout << "find it" << endl;
    //反转
    reverse(test.begin(), test.end());
    return 0;
}

 

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