list删除指定元素

#include 
#include 
#include 
using namespace std;
int main()
{
    list<int> List;
    List.push_back(1);
    List.push_back(1);
    List.push_back(3);
    List.push_back(1);
    List.push_back(1);
    List.push_back(5);
    List.push_back(1);
    List.push_back(1);
    for (list<int>::iterator i = List.begin(); i != List.end();) {
        if((*i) == 1){
            List.erase(i++);
        }else
        {
            i++;
        }
    }
    for (list<int>::const_iterator i = List.begin(); i != List.end(); ++i) {
        if(i != List.begin())
        {
            cout <<" ";
        }
        cout << *i;
    }

    return 0;
}


你可能感兴趣的:(c++)