STL中的函数对象(一)
来源:互联网 发布:服务器数据备份方案 编辑:程序博客网 时间:2024/06/05 14:37
为使类属性算法具有灵活性,STL常用函数重载机制为算法提供两种形式,算法的第一种形式使用的是常规操作来实现目标。在第二种形式中,算法可以根据用户指定的准则对元素进行处理。这种准则是通过函数对象来传递的。函数对象世纪上是重载了operator()的类模版。
STL提供了许多函数对象,这些对象包含在头文件<functional>中。
STL中包含许多预定义的函数对象,其中有:
- 算术操作:plus, minus, multiplies, divides, modulus, 和 negate,例:
assert(V2.size() >= V1.size() && V3.size() >= V1.size());
transform(V1.begin(), V1.end(), V2.begin(), V3.begin(),modulus<int>());把V1[i]%V2[i]存入V3[i]。
- 比较操作:equal_to, not_equal_to, greater, less, greater_equal, 和 less_equal,例:
list<int>::iterator first_nonnegative = find_if(L.begin(), L.end(), bind2nd(greater_equal<int>(), 0));
在L中找到第一个大于等于0的元素。
- 逻辑操作:logical_and, logical_or, 和 logical_not,例:
char str[MAXLEN];
const char* wptr = find_if(str, str + MAXLEN,compose2(logical_or<bool>(),bind2nd(equal_to<char>(), ' '),bind2nd(equal_to<char>(), '\n')));在str中找到第一个空格或行尾。
以上三类都是模板类,要生成一个函数对象需要实例化。同时在使用时,需要注意:
- 算法需要的参数是什么类型的函数对象,是一元函数还是二元函数。
- 输入容器内元素类型是否与函数对象参数兼容,输出容器内元素是否与函数对象返回值兼容。
- 修改、组装已有函数对象生成新的函数对象:unary_compose, binary_compose, unary_negate, binary_negate, binder1st, binder2nd, pointer_to_unary_function, pointer_to_binary_function, mem_fun_t, mem_fun_ref_t, mem_fun1_t 和mem_fun1_ref_t。这类函数对象就是所谓的函数对象适配器,由于书写生涩,一般不会直接实例化,都有对应的辅助函数(一般都是内联型)简化调用。
const AdaptableUnaryFunction2& g);
生成一个一元函数对象,操作为“f(g(x))”;vc6/7自带stl没有定义。
binary_composecompose2binary_compose(const AdaptableBinaryFunction& f,const AdaptableUnaryFunction1& g1,
const AdaptableUnaryFunction1& g2);
生成一个二元函数对象,操作为“f(g1(x), g2(x))”;vc6/7自带stl没有定义。
unary_negatenot1unary_negate(const AdaptablePredicate& pred);一元谓词取非。
binary_negatenot2binary_negate(const AdaptableBinaryPredicate& pred);二元谓词取非。
binder1stbind1stbinder1st(const AdaptableBinaryFunction& F,AdaptableBinaryFunction::first_argument_type c);
把二元函数对象的第一个参数绑定为c,生成一个一元函数对象。
binder2nd bind2ndbinder2nd(const AdaptableBinaryFunction& F,AdaptableBinaryFunction::second_argument_type c);
把二元函数对象的第二个参数绑定为c,生成一个一元函数对象。
pointer_to_unary_functionptr_fun把普通全局函数或全局函数指针(只能有1个或2个参数)转换成函数对象。ptr_fun有两个原型分别生成pointer_to_unary_function对象和pointer_to_binary_function对象。
pointer_to_binary_functionmem_fun_tmem_fun把某个类的成员函数(只能有0个或1个参数,因为类成员函数的第一个参数已定义为this指针)转换成函数对象。mem_fun有两个原型分别生成mem_fun_t对象(是个一元函数对象)和mem_fun1_t对象(是个二元函数对象)。
mem_fun1_tmem_fun_ref_tmem_fun_ref类似mem_fun,区别如下:struct T{
print(){}
};
list<T*> lstT;
for_each(lstT.begin(),lstT.end(),mem_fun(&T::print));
list<T> lstT;
for_each(lstT.begin(),lstT.end(),mem_fun_ref(&T::print));
- STL中的函数对象(一)
- STL中的函数对象
- STL中的函数对象
- STL中的函数对象
- STL中的函数对象(Funciont Objects)
- STL中的函数对象(Funciont Objects)
- STL中的函数对象(Funciont Objects)
- STL中的函数对象(Funciont Objects)
- STL函数对象:定义、及其在STL中的应用
- C++手稿:STL中的函数对象与函数指针
- 使用STL中的泛型算法及函数对象
- STL--函数对象
- STL函数对象
- STL之函数对象
- STL 函数对象
- [STL基础]函数对象
- STL 函数对象
- STL函数对象
- Jquery extend 函数的 用法 解析
- 来发背包开开胃(OJ--3303
- 在ubuntu下使用Eclipse搭建Hadoop开发环境《转》
- 快排算法,以及 top_k应用
- 黑马程序员 网络编程
- STL中的函数对象(一)
- CodeForces 389A Fox and Number Game
- 使用HttpURLConnection下载图片
- TCP/IP网络常用名词缩写
- input中加入图片的做法
- UI 08 tableView版中国省市区 -- 3页
- 杭电 HDU ACM 2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(多重背包)
- 社説 20150829 新国立競技場 五輪へ失策はもう許されない
- Java 温故