erase vector

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <list>
#include <algorithm>    //to use remove_if
using namespace std;


typedef vector<int> V;
typedef vector<int>::iterator VIT;
typedef list<int> L;
typedef list<int>::iterator LIT;
V v;
L lis;


class InList
{
public:
    InList(list<int> &lis):li(lis){}

    bool operator()(int n)
    {
        return find(li.begin(), li.end(), n)!=li.end();
    }
private:
    list<int> &li;

};

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class MyClass
{
public:
    MyClass(int v){ m_value=v;}
    virtual ~MyClass(){}

    bool  operator==(const MyClass& otherData);
    bool operator!=(const MyClass &right) const
    {
        return !(this==&right);//调用重载运算符==
    }
MyClass  operator=(const MyClass& otherData);
    int m_value;
}
#if 1
MyClass::operator =(const MyClass &otherData)
{
    if(*this != otherData)
{
        m_value=otherData.m_value;
}
    
    return *this;
}

bool MyClass::operator ==(const MyClass &otherData)
{
    if (otherData.m_value==this->m_value)
    {
        return true;
    }else
    {
        return false;
    }
}
#endif

int main()
{
    std::vector<MyClass> myVector;
    MyClass v1(1);
    MyClass v2(2);
    MyClass v3(3);
    myVector.push_back(v1);
    myVector.push_back(v2);
    myVector.push_back(v3);

    for( std::vector<MyClass>::iterator result=myVector.begin();
        result!=myVector.end();
        ++result)
    {
        cout << "Element " << result->m_value << endl;
    }

    MyClass v(2);
    std::vector<MyClass>::iterator it = std::find(myVector.begin(),myVector.end(),v);
    if (it!=myVector.end())
    {
        myVector.erase(it);
    }
    for( std::vector<MyClass>::iterator result=myVector.begin();
        result!=myVector.end();
        ++result)
    {
        cout << "After erase MyClass v(2), Element left " << result->m_value << endl;
    }
    return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class MyClass
{
public:
    MyClass(int v){ m_value=v;}
    virtual ~MyClass(){}

    bool  operator==(const MyClass& otherData);
    bool operator!=(const MyClass &right) const
    {
        return !(this==&right);//调用重载运算符==
    }
MyClass  operator=(const MyClass& otherData);
    int m_value;
}
#if 1
MyClass::operator =(const MyClass &otherData)
{
    if(*this != otherData)
{
        m_value=otherData.m_value;
}
    
    return *this;
}

bool MyClass::operator ==(const MyClass &otherData)
{
    if (otherData.m_value==this->m_value)
    {
        return true;
    }else
    {
        return false;
    }
}
#endif

int main()
{
    std::vector<MyClass*> myVector;
MyClass *p=NULL;
p=new MyClass(1);
myVector.push_back(p);
p=new MyClass(2);
myVector.push_back(p);
p=new MyClass(3);
myVector.push_back(p);
    
    for( std::vector<MyClass*>::iterator result=myVector.begin();
        result!=myVector.end();
        ++result)
    {
        cout << "Element " <<( *result)->m_value << endl;
    }
#if 1
    MyClass v(2);
    std::vector<MyClass*>::iterator it = std::find(myVector.begin(),myVector.end(),&v);
    if (it!=myVector.end())
    {
        myVector.erase(it);
    }
    for( std::vector<MyClass*>::iterator result=myVector.begin();
        result!=myVector.end();
        )
    {
        if ((* result)->m_value==2)
        {delete(*result);
        result=myVector.erase(result);
        result;}
        else
        {
            ++result;

        }
        
        
    }
for( std::vector<MyClass*>::iterator result=myVector.begin();
    result!=myVector.end();
    ++result)
{
    cout << "After erase MyClass v(2), Element left " <<(* result)->m_value << endl;
}
#endif
    return 0;
}













#define DEMO3
int main()
{
    for(int i=0; i<10; ++i) // 初始化v: 0 1 2 3 4 5 6 7 8 9
        v.push_back(i);
    for(int i=0; i<5; ++i)  // 初始化lis:0 1 2 3 4
        lis.push_back(i);

    VIT it;
    cout<<endl<<v.end()-v.begin()<<endl;  //vector容器的iterator是支持加减操作的,这在其他类型的迭代器中很少见

#ifdef DEMO1
    //遍历删除v中不等于3的元素的正确方法
    for(it=v.begin(); it!=v.end();)
    {
        if(*it != 3)
        {
            //将it赋值为erase()的返回值,它指向the new location of the element that followed the last element erased
            it = v.erase(it);
        }
        else
            ++it;
    }
    for(it=v.begin(); it!=v.end(); ++it)
        cout<<*it<<endl;
#endif

#ifdef DEMO2
    //DEMO2的功能: 在v中,删除那些在v和在lis中同时存在的元素,不使用remove_if
    it = v.begin();
    VIT del = it;
    for(; it!=v.end(); ++it)
    {
        if(find(lis.begin(),lis.end(),*it) == lis.end())
        {
                cout<<*del<<"....."<<endl;
            *del++ = *it;
        
        }
    }//此时,del指向A iterator pointing to the new end of the sequence,which now includes all the elements which is not in lis.
    //整个v变为 5 6 7 8 9 5 6 7 8 9,其中,del指向第二个5
    v.erase(del, v.end());
    for(it=v.begin(); it!=v.end(); ++it)
        cout<<*it<<endl;
#endif

#ifdef DEMO3
    //DEMO2的功能: 在v中,删除那些在v和在lis中同时存在的元素,使用remove_if

    /*
    //如果先单独运行remove_if,则v会变为5 6 7 8 9 5 6 7 8 9
    //即其前五个值由0 1 2 3 4 变为5 6 7 8 9,这与remove_if的实现有关
    remove_if(v.begin(), v.end(), InList(lis));
    for(it=v.begin(); it!=v.end(); ++it)
    cout<<*it<<endl;
    */

    //remove_if的返回值是A forward iterator pointing to the new end of the sequence,
    //which now includes all the elements for which InList(lis) was false.
    v.erase(remove_if(v.begin(), v.end(), InList(lis)), v.end());
    for(it=v.begin(); it!=v.end(); ++it)    //此时v变为5 6 7 8 9, 0 1 2 3 4被删除
        cout<<*it<<endl;
#endif

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值