C++ functor
来源:互联网 发布:商城网络平台建设公司 编辑:程序博客网 时间:2024/05/22 04:40
functor就是一个重载了 operator()的类,用这个类生成的实例就像一个函数。(functor就是一个作为函数用的类),在c++11后可以用lambda函数实现同样的功能。
参考链接:stackoverflow
// this is a functorstruct add_x { add_x(int x) : x(x) {} int operator()(int y) const { return x + y; }private: int x;};// 这也是一个functorstruct inc{ int operator()(int _i) { return _i + 1;}};// Now you can use it like this:add_x add42(42); // create an instance of the functor classint i = add42(8); // and "call" itassert(i == 50); // and it added 42 to its argument, 检查i是否等于50std::vector<int> in; // assume this contains a bunch of values)std::vector<int> out(in.size());// Pass a functor to std::transform, which calls the functor on every element // in the input sequence, and stores the result to the output sequence// add_x(1), 相当于创建了 add_x add1(1)// add1 相当于一个函数,传入一个int参数, 这个int 会加上 1std::transform(in.begin(), in.end(), out.begin(), add_x(1)); //相当于inc i_1;std::transform(in.begin(), in.end(), out.begin(), i_1); // 效果和下面这句相同std::transform(in.begin(), in.end(), out.begin(), [](int x){ return x + 1;});assert(out[i] == in[i] + 1); // for all i
阅读全文
0 0
- 内联函数/回调函数/仿函数(functor)-------(c++)
- 内联函数/回调函数/仿函数(functor)-------(c++)
- template functor
- haskell Functor
- functor 是什么?
- C++ functor
- [C/C++标准库]_[初级]_[函数对象functor的使用]
- C++STL学习(9)仿函数(function objects, functor)
- functor(function object)
- Functor——转发
- Functor——Impl
- Functor——bind
- Functor——周边
- function object (functor) (1)
- Functor 仿函数
- C++ 仿函数(functor)
- 函数对象(functor)
- STL仿函数functor
- js空连接点击不跳转
- CentOS6.8 SVN服务器管理多项目
- Linux下安装 Kafka
- CSS3 background属性值
- c++ 输入输出优化
- C++ functor
- Linux下的路由表详解
- 详解Javascript中正则表达式的使用
- NAT的四种类型及类型检测
- 模型融合方法总结
- 初见蓝桥——历年试题 最大子阵
- ios 证书生成
- 网页换肤(简洁写法[提供思路])
- ansible