stl中的谓词函数
各位大侠,最近在学习STL,使用LIST::REMOVE_IF函数时遇到了一些问题,代码如下:
#include<iostream>
#include<list>
using namespace std;
bool Compare( int i )
{
return ( i >= 3 && i <= 8 );
}
void Print( list<int> &ivec )
{
list<int>::const_iterator it = ivec.begin(), it_end = ivec.end();
for( ; it != it_end; ++ it )
cout << *it << ' ';
cout << endl;
}
int main()
{
int ia[] = { 1, 3, 9, 4, 5, 34, 2 };
list<int> ivec( ia, ia + 7 );
Print( ivec );
ivec.remove_if(Compare); //去掉3和8之间的元素
Print( ivec );
system("Pause");
return 0;
}
1、关于List的remove_if函数的参数不明白?参考手册上的解释如下:
void remove_if( UnPred pr );
remove_if()以一元谓词pr为判断元素的依据,遍历整个链表。如果pr返回true则删除该元素。
那么谓词是什么东东?应该怎么定义?怎么使用?我查了一些资料,说法为:“做某些检测的函数,配合标准算法库使用”,不过对于其定义和使用没有一个规范的说法?我还是没有明白?
2、上述的代码我在VC6.0+sp6中无法编译通过,错误提示:
error C2664: 'remove_if' : cannot convert parameter 1 from 'bool (const double &)' to 'class std::binder2nd<struct std::not_equal_to<double> >'
这里的class std::binder2nd<struct std::not_equal_to<double> >是个什么类型啊?能不能详细说明一下!
我在其它的一些资料中查到:“谓词即为元素存储和检索的描述,如std::less<>,std::greater<>那么就按降序/升序排列,你也可以定义自己的谓词”,同时std::binder2nd我发现是捆绑器的意思,不过这个东东和谓词的关系是什么,怎么使用?
我想好好学学这些东东,请各位提供一些好的书籍或是资料,非常感谢!
3、上述的代码我在VS2005中运行无误,这是怎么回事儿啊?难道是VC6.0对于STL的支持有问题?请说说原因,或是改正方法。
分数不多了,请见谅,非常感谢!