c++ bind1st 和 bind2nd的用法
c++ bind1st 和 bind2nd的用法
来源: http://www.cnblogs.com/renyuan/p/6216375.html
std::bind1st 和 std::bind2nd将二元函数转换为一元函数,具体用法参加下面的代码。
代码介绍了两种使用方式,第一种是使用std::less和std::greater,第二种是使用自定义的仿函数。
#include <iostream> #include <vector> #include <string> #include <iterator> #include <algorithm> #include <functional> /** * std::bind1st std::bind2nd 就是将一个二元函数的一个参数设置为定值,这样二元函数就转换为了一元函数 * 因为有些算法的参数要求必须是一元函数,但是我们又想用二元函数,那么就可以使用这两个函数 */ /** *@brief std::less 仿函数的内部实现 template <class T> struct less : binary_function <T,T,bool> { bool operator() (const T& x, const T& y) const {return x<y;} }; */ struct person{ int age; std::string name; }; struct person_filter_func: public std::binary_function<person,std::string,bool> { bool operator()(const person& p,const std::string& key) const{ return (p.name.find(key) != std::string::npos); } }; void disp(int val){ std::cout<<val<<std::endl; } void disp_v(const person& p){ std::cout<<p.age<<","<<p.name<<std::endl; } int main() { //使用 std::less 仿函数 int arr[] = {1,2,3,4,5,6,7,8,9}; std::vector<int> vec; std::copy_if(std::begin(arr),std::end(arr),std::back_inserter(vec),std::bind1st(std::less<int>(),6)); //将6 绑定为第一个参数,即 6 < value std::for_each(vec.begin(),vec.end(),disp); // 7 8 9 vec.clear(); std::copy_if(std::begin(arr),std::end(arr),std::back_inserter(vec),std::bind2nd(std::less<int>(),6)); //将6 绑定为第二个参数,即 value < 6 std::for_each(vec.begin(),vec.end(),disp); //1 2 3 4 5 //使用自定义的仿函数 std::vector<person> vecP; person p1 = {1,"jack"}; vecP.push_back(p1); person p2 = {2,"rose"}; vecP.push_back(p2); person p3 = {3,"jane"}; vecP.push_back(p3); std::vector<person> vecRet; std::copy_if(vecP.begin(),vecP.end(),std::back_inserter(vecRet),std::bind2nd(person_filter_func(),"ja")); //将包含关键字"ja"的person,复制到vecRet容器中 std::for_each(vecRet.begin(),vecRet.end(),disp_v);//1, jack 3, jane }
copy_if:
template <class InputIterator, class OutputIterator, class UnaryPredicate> OutputIterator copy_if (InputIterator first, InputIterator last, OutputIterator result, UnaryPredicate pred) { while (first!=last) { if (pred(*first)) { *result = *first; ++result; } ++first; } return result; }
std::bind1st
template <class Operation, class T> binder1st<Operation> bind1st (const Operation& op, const T& x) { return binder1st<Operation>(op, typename Operation::first_argument_type(x)); }
std::binder1st
template <class Operation> class binder1st : public unary_function <typename Operation::second_argument_type, typename Operation::result_type> { protected: Operation op; typename Operation::first_argument_type value; public: binder1st ( const Operation& x, const typename Operation::first_argument_type& y) : op (x), value(y) {} typename Operation::result_type operator() (const typename Operation::second_argument_type& x) const { return op(value,x); } };
std::remove_if
template <class ForwardIterator, class UnaryPredicate> ForwardIterator remove_if (ForwardIterator first, ForwardIterator last, UnaryPredicate pred) { ForwardIterator result = first; while (first!=last) { if (!pred(*first)) { *result = std::move(*first); ++result; } ++first; } return result; }
分类:
c++
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南