wly603

从STL中的list删除元素

主要是注意删除末尾的元素时,需要注意 

测试代码
#include <iostream>
#include <list>
#include <algorithm>
using namespace  std;


int main(int argc, char* argv[])
{
    list<int> MyList;
    
    for (int i = 0; i < 10; i++)
    {
        MyList.push_back(i);
    }
    
    list<int>::iterator Itor;
    
    for ( Itor = MyList.begin(); Itor != MyList.end(); )
    {
        if ( *Itor == 4 )
        {
            Itor = MyList.erase(Itor);
        }
        else
        {
            Itor++;
        }
    }
    
//    copy(MyList.begin(), MyList.end(), ostream_iterator<int>(cout, " ") );
    for(Itor=MyList.begin();Itor!=MyList.end();Itor++)
    {
        cout<<*Itor<<"  ";
    }

    cout<<endl;
    
    return 0;
}

 

 

posted on 2012-04-11 10:45  wly603  阅读(240)  评论(0编辑  收藏  举报

导航