remove_if的问题
#include<iostream>
#include<list>
#include<algorithm>
#include"PRINT_ELEMENTS.h" using namespace std; class Nth{
private:
int nth;
int count;
public:
Nth(int n):nth(n),count(){
}
bool operator()(int){
return ++count==nth;
}
}; int main(int argc, char **argv)
{
list<int> coll;
for(int i=; i<=;++i){
coll.push_back(i);
}
PRINT_ELEMENTS(coll,"initialized: "); list<int>::iterator pos = remove_if(coll.begin(),coll.end(),Nth());
coll.erase(pos, coll.end());
PRINT_ELEMENTS(coll, "remove_if: ");
return ;
}
//PRINT_ELEMENTS.h
template<typename T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
typename T::const_iterator pos;
std::cout << optcstr << std::endl;
for(pos = coll.begin(); pos != coll.end(); ++pos)
std::cout << *pos << " ";
std::cout << std::endl;
}
输出结果:
initialized:
remove_if:
而不是:
initialized:
remove_if:
此处是remove_if的问题:
remove_if源代码为:
template<typename ForwIter, typename Predicate>
ForwIter remove_if(ForwIter beg, ForwIter end, Predicate op)
{
beg=find_if(beg, end, op) if(beg == end)
{
return beg;
}
else
{
ForwIter next = beg;
return remove_copy_if(++next, end, beg, op);
}
}
这里find_if调用op是复制了一个op的副本,所以当remove_copy_if再次调用op是用重新计数了,所以也把6删除了;
可以用:
#include<iostream>
#include<list>
#include<algorithm>
#include"PRINT_ELEMENTS.h" using namespace std; class Nth{
private:
int nth;
int count;
public:
Nth(int n):nth(n),count(){
}
bool operator()(int){
return ++count==nth;
}
}; template<typename ForwIter, typename Predicate>
ForwIter remove_if_my(ForwIter beg, ForwIter end, Predicate op)
{
while(beg != end && !op(*beg))
{
++beg;
} if(beg == end)
{
return beg;
}
else
{
ForwIter next = beg;
return remove_copy_if(++next, end, beg, op);
}
}
int main(int argc, char **argv)
{
list<int> coll;
for(int i=; i<=;++i){
coll.push_back(i);
}
PRINT_ELEMENTS(coll,"initialized: "); list<int>::iterator pos = remove_if_my(coll.begin(),coll.end(),Nth());
coll.erase(pos, coll.end());
PRINT_ELEMENTS(coll, "remove_if: ");
return ;
}
这样避去用find_if,所以就可以了。
参考:《C++标准程序库》P302
remove_if的问题的更多相关文章
- std::remove_if
原型: #include <algorithm>forward_iterator remove_if( forward_iterator start, forward_iterator e ...
- C++ count_if/erase/remove_if 用法详解
每次使用这几个算法时都要去查CPP reference,为了能够加深印象,整理一下基本应用. cout/cout_if: return the number of elements satisfyi ...
- STL --> remove和remove_if()
remove和remove_if() 一.Remove()函数 remove(beg,end,const T& value) //移除区间{beg,end)中每一个“与value相等”的元素: ...
- 移除元素(remove,remove_if...unique...)
remove 因为本算法作用的是iterator,所以并不会改变Container大小,会返回一个新的iterator new_last,是的first到new_last中的元素都不等于value,左 ...
- 【C++】STL算法之remove_if
之前写过这样一段代码: auto iter=remove_if(AllEdges.begin(),AllEdges.end(),[&](Edge* edge){return _isEedge( ...
- c++ remove_if
#include <algorithm> 函数remove_if()移除序列[start, end)中所有应用于谓词p返回true的元素. 此函数返回一个指向被修剪的序列的最后一个元素迭代 ...
- C++之remove和remove_if
一.Remove()函数 remove(beg,end,const T& value) //移除区间{beg,end)中每一个“与value相等”的元素: remove只是通过迭代器的指针向前 ...
- erase & remove_if 合用
words_.erase( remove_if( words_.begin(), words_.end(), [&](const entry& e) { return (e.type ...
- C++ remove remove_if erase
#include <iostream>#include <algorithm>#include <list>#include <vector>#incl ...
随机推荐
- shell-code-5-函数
# 函数必须在使用前定义 # 如果不写return,将以最后一条命令运行结果,作为返回值. return后跟数值n(0-255) myFistFunc(){ read a read b return ...
- DSP中-stack和-heap的作用
-stack 0x00000800-heap 0x00000800 stack - 又称系统栈(system stack),用于: 保存函数调用后的返回地址; ...
- PAT Basic 1071
1071 小赌怡情 常言道“小赌怡情”.这是一个很简单的小游戏:首先由计算机给出第一个整数:然后玩家下注赌第二个整数将会比第一个数大还是小:玩家下注 t 个筹码后,计算机给出第二个数.若玩家猜对了,则 ...
- Android兼容性测试GTS-环境搭建、测试执行、结果分析
GTS的全称是Google Mobile Services Test Suite,所谓的Google Mobile Services即谷歌移动服务,是谷歌开发并推动Android的动力,也是Andro ...
- apk 解包 打包
APK应用程序的解包.修改.编辑.汉化.打包及应用 前两讲主要讲玩机的最基本的知识,集中在如何刷机.本讲是进级的内容,来谈谈与apk应用程序有关的知识,内容包括akp文件的解包.打包.反编辑.解析.汉 ...
- c#中的String方法
1.Replace(替换字符):public string Replace(char oldChar,char newChar);在对象中寻找oldChar,如果寻找到,就用newChar将oldCh ...
- 使用 RMAN 同步数据库
使用 RMAN同步数据库 使用 RMAN 同步数据库 一.概述 二 操作步骤 (一).把生产库置为归档模式 (二).启动rman做数据库0级备份 (三).修改生产库数据库到未归档 (四).拷贝备份集到 ...
- Knockout v3.4.0 中文版教程-3-监控-通过监控创建视图模型(下)
6. 显式订阅监控 你通常不需要手动设置订阅,所以初学者应该跳过这一节. 对于高级用户,如果你想注册自己的订阅来监控通知变化,你可以使用 subscribe函数,比如: myViewModel.per ...
- android 之 ListView相关
ListView是一种列表视图,其将ListAdapter所提供的各个控件显示在一个垂直且可滚动的列表中.需要注意的为创建适配器并将其设置给ListView. 1.ArrayAdapter Array ...
- Java包(package)详解
java包的作用是为了区别类名的命名空间 1.把功能相似或相关的类或接口组织在同一个包中,方便类的查找和使用.. 2.如同文件夹一样,包也采用了树形目录的存储方式.同一个包中的类名字是不同的,不同的包 ...