STL仿函数functor
来源:互联网 发布:use衣服知乎 编辑:程序博客网 时间:2024/05/12 14:14
1.仿函数的妙处(P127)
示例:
//一般函数void fo(){statements}//仿函数class FunctionObjectType{public:void operator() (){statements}};
- 仿函数比一般函数更灵巧。(可以拥有状态(state))
- 每个仿函数都有其型别。导致:容器型别也会因为仿函数的不同而不同。
- 执行速度上,仿函数通常比函数指针更快。
#include <list>#include <algorithm>#include <iostream>#include <iterator>using namespace std;template <class T>inline void PRINT_ELEMENTS(const T& coll, const char* optcstr=" "){typename T::const_iterator pos;std::cout<<optcstr;for(pos=coll.begin();pos!=coll.end();++pos){std::cout<<*pos<<' ';}std::cout<<std::endl;}//传值class IntSequence{private:int value;public://constructorIntSequence(int initialValue):value(initialValue){}//"function call"int operator() (){return value++;}};//传引用class IntSequenceRef{private:int& value;public://constructorIntSequenceRef(int& initialValue):value(initialValue){}//"function call"int operator() (){return value++;}};//传值和传引用模板类template<typename T>class IntSeque{private:T value;public://constructorIntSeque(T initialValue):value(initialValue){}//"function call"int operator() (){return value++;}};int main(){list<int> coll1;cout<<"coll1:(传值)"<<endl;generate_n(back_inserter(coll1),//start9,//number of elementsIntSequence(1));//generates valuesPRINT_ELEMENTS(coll1);generate(++coll1.begin(),//start--coll1.end(),//endIntSequence(42));//generate valuesPRINT_ELEMENTS(coll1);cout<<"coll2:(传引用)"<<endl;list<int> coll2;int value = 2;IntSequenceRef seq(value);generate_n (back_inserter(coll2),4,seq);PRINT_ELEMENTS(coll2);int k=10;generate_n(back_inserter(coll2),4,IntSequenceRef(k));PRINT_ELEMENTS(coll2);cout<<"k="<<k<<endl;generate_n(back_inserter(coll2),4,seq);PRINT_ELEMENTS(coll2);cout<<"coll3:(模板类)"<<endl;int refvalue=4;IntSeque<int&>SeRef(refvalue);IntSeque<int>Se(4);list<int>coll3;generate_n(back_inserter(coll3),4,SeRef);PRINT_ELEMENTS(coll3);generate_n(back_inserter(coll3),4,SeRef);PRINT_ELEMENTS(coll3);generate_n(back_inserter(coll3),4,Se);PRINT_ELEMENTS(coll3);generate_n(back_inserter(coll3),4,Se);PRINT_ELEMENTS(coll3);}
2.函数配接器
2.1含义:
指能够将仿函数和另一个仿函数(或某个值,或某个一般函数)结合起来的仿函数。声明于<functional>。函数配接器本身也是仿函数。
2.2针对成员函数而设计的函数配接器
mem_fun_ref(op) 调用op,某对象的一个const成员函数。
mem_fun(op) 调用op,某对象指针的一个const成员函数。
注:C++标准程序库并不针对non-const成员函数提供函数配接器。mem_fun_ref和mem_fun只能以无参数或单参数方式来调用成员函数。
#include <algorithm>#include <iostream>#include <vector>#include <string>using namespace std;class Person{private:string name;public:Person(string str):name(str){}void setperson(string n){name=n;}void print() const{cout<<name<<endl;}void printwithprefix(string prefix) const{cout<<prefix<<name<<endl;}};int main(){vector<Person> coll;Person p("0");for(int i=1;i<=8;++i){string name(3,'a'+i);p.setperson(name);coll.push_back(p);}for_each(coll.begin(),coll.end(),mem_fun_ref(&Person::print));for_each(coll.begin(),coll.end(),bind2nd(mem_fun_ref(&Person::printwithprefix),"Person: "));}
对于一般函数(非成员函数)可以使用prt_fun(op),ptr_fun允许在其他函数配接器中使用一般函数。
例如:pos = find_if(coll.begin(), coll.end(), not1(ptr_fun(check)));//bool check(int elem);
- STL仿函数functor
- stl中的仿函数functor的应用
- STL学习笔记(四):仿函数(functor)
- C++ STL 学习 :for_each与仿函数(functor)
- C++ STL 学习 :for_each与仿函数(functor)
- C++ STL 学习 :for_each与仿函数(functor)(一)
- C++ STL 学习 :for_each与仿函数(functor)
- C++ STL 学习 :for_each与仿函数(functor)
- C++ STL 学习 :for_each与仿函数(functor)(一)
- C++ STL 学习 :更多仿函数(functor)(二)
- Functor 仿函数
- C++ 仿函数(functor)
- C++ 仿函数(functor)
- C++ 仿函数(functor)
- functor 仿函数
- functor(仿函数)初涉
- 仿函数functor
- 仿函数(functor)
- 号外:Instagram曾狮子大开口 叫价20亿美元
- Maven
- What are the Microsoft Office MIME Types
- 在ubuntu下networking disable的解决方法
- struts2 JSON日期转化注解
- STL仿函数functor
- 第九周任务1
- Mongodb的带用户验证replica set配置
- Efficient auto-complete with a ternary search tree
- 第八周实验报告(1-1)
- C++程序设计实验报告(9-4)
- 数据库 insert 用法实例<二>
- Basic Skill: Arithmetic
- android bug fix