删除容器对象的几种方式

●去除一个容器中有特定值的所有对象: 

1、如果容器是vector、string或deque,使用erase-remove惯用法。

Container<int> c; 

c.erase(remove(c.begin(), c.end(), 1963),               // 当c是vector、string
                c.end());                               // 或deque时,
                                                // erase-remove惯用法
                                                // 是去除特定值的元素
                                                // 的最佳方法

2、如果容器是list,使用list::remove。

c.remove(1963);         // 当c是list时,
                        // remove成员函数是去除
                        // 特定值的元素的最佳方法

3、如果容器是标准关联容器(即,set、multiset、map或multimap),使用它的erase成员函数。

c.erase(1963);          // 当c是标准关联容器时
                        // erase成员函数是去除
                        // 特定值的元素的最佳方法



● 去除一个容器中满足一个特定判定式的所有对象:

bool badValue(int x);   // 返回x是否是“bad” 

1、如果容器是vector、string或deque,使用erase-remove_if惯用法。

c.erase(remove_if(c.begin(), c.end(), badValue),       // 当c是vector、string
                        c.end());                       // 或deque时这是去掉
                                                // badValue返回真
                                                // 的对象的最佳方法


2、如果容器是list,使用list::remove_if。

c.remove_if(badValue);             // 当c是list时这是去掉
                                                // badValue返回真
                                                // 的对象的最佳方法


3、如果容器是标准关联容器,使用remove_copy_if和swap,或写一个循环来遍历容器元素,当你把迭代器传给erase时记得后置递增它。

AssocContainer<int> c;
...
for (AssocContainer<int>::iterator i = c.begin();       // for循环的第三部分
        i != c.end();                           // 是空的;i现在在下面
        /*nothing*/ ){                           // 自增
        if (badValue(*i))  c.erase(i++);         // 对于坏的值,把当前的
        else ++i;                                       // i传给erase,然后
}                                               // 作为副作用增加i;
                                                // 对于好的值,
                                                // 只增加i


● 在循环内做某些事情(除了删除对象之外): 

1、如果容器是标准序列容器,写一个循环来遍历容器元素,每当调用erase时记得都用它的返回值更新你的迭代器。

for (SeqContainer<int>::iterator i = c.begin(); 
        i != c.end(); /*nothing*/  ){
        if (badValue(*i)){
                logFile << "Erasing " << *i << '\n'; 
                i = c.erase(i);                 // 通过把erase的返回值
        }                                       // 赋给i来保持i有效
        else ++i;
}


如果容器是标准关联容器,写一个循环来遍历容器元素,当你把迭代器传给erase时记得后置递增它。

ofstream logFile;                                       // 要写入的日志文件
AssocContainer<int> c;
...
for (AssocContainer<int>::iterator i = c.begin();       // 循环条件和前面一样
        i !=c.end();){
        if (badValue(*i)){ 
                logFile << "Erasing " << *i <<'\n';     // 写日志文件 
                c.erase(i++);                   // 删除元素
        }
        else ++i;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值