c++ 列表删除元素(erase)

 

#include <list>
#include <iostream>
#include <iterator>
using namespace std;
int main( )
{
    list<int> c{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    for (auto &i : c) {
        cout << i << " ";
    }
    cout << '\n';
    c.erase(c.begin());//删除第一个元素
    for (auto &i : c) {
        cout << i << " ";
    }
    cout << '\n';
    list<int>::iterator range_begin = c.begin();
    list<int>::iterator range_end = c.begin();
    advance(range_begin,2);
    advance(range_end,5);
    c.erase(range_begin, range_end);
    for (auto &i : c) {
        cout << i << " ";
    }
    cout << '\n';
    // Erase all even numbers (C++11 and later)
    for (auto it = c.begin(); it != c.end(); ) {
        if (*it % 2 == 0) {
            it = c.erase(it);//删除偶数
        } else {
            ++it;
        }
    }
    for (auto &i : c) {
        cout << i << " ";
    }
    cout << '\n';
}

 

原文链接: https://www.cnblogs.com/sea-stream/p/10110904.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    c++ 列表删除元素(erase)

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/286768

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(1)
上一篇 2023年2月15日 上午9:34
下一篇 2023年2月15日 上午9:35

相关推荐