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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM