stl源码剖析 详细学习笔记 仿函数
//---------------------------15/04/01----------------------------
//仿函数是为了算法而诞生的,可以作为算法的一个参数,来自定义各种操作,比如比大小,返回bool值,对元素进行操作等
//虽然这些函数也能实现,但是如果配合配接器(adapter)可以产生更灵活的变化。
//为了使对象像函数一样,就必须重载operator()
//unary_function
template<class Arg, class Result>
struct unary_function
{
//参数类型
typedef Arg argument_type;
//返回值类型
typedef Result result_type;
};
//binary_functione
//二元仿函数
template<class Arg1, class Arg2, class Result>
struct binary_functione
{
typedef Arg1 first_argument_type;
typedef arg2 second_argument_type;
typedef Result result_type;
};
//算术类仿函数
template<class T>
struct plus : public binary_functione<T, T, T>
{
T operator()(const T& x, const T& y) const
{
return x + y;
}
};
template<class T>
struct minus : public binary_functione<T, T, T>
{
T operator()(const T& x, const T& y) const
{
return x - y;
}
};
template<class T>
struct multiplies : public binary_functione<T, T, T>
{
T operator()(const T& x, const T& y) const
{
return x * y;
}
};
template<class T>
struct divides : public binary_functione<T, T, T>
{
T operator()(const T& x, const T& y) const
{
return x / y;
}
};
template<class T>
struct modulus : public binary_functione<T, T, T>
{
T operator()(const T& x, const T& y) const
{
return x & y;
}
};
template<class T>
struct negate: public unary_function<T, T>
{
T operator()(const T& x) const
{
return -x;
}
};
//证同元素,数值a与该元素做op操作会得到自己。 加法的证同元素为0 乘法为1
template<class T>
inline T identity_element(plus<T>)
{
return T(0);
};
template<class T>
inline T identity_element(multiplies<T>)
{
return T(1);
};
//关系运算类仿函数
template<class T>
struct equal_to : public binary_functione<T, T, bool>
{
bool operator()(const T& x, const T& y) const
{
return x == y;
}
};
template<class T>
struct not_equal_to : public binary_functione<T, T, bool>
{
bool operator()(const T& x, const T& y) const
{
return x != y;
}
};
template<class T>
struct greater : public binary_functione<T, T, bool>
{
bool operator()(const T& x, const T& y) const
{
return x > y;
}
};
template<class T>
struct less : public binary_functione<T, T, bool>
{
bool operator()(const T& x, const T& y) const
{
return x < y;
}
};
template<class T>
struct greater_equal : public binary_functione<T, T, bool>
{
bool operator()(const T& x, const T& y) const
{
return x >= y;
}
};
template<class T>
struct less_equal : public binary_functione<T, T, bool>
{
bool operator()(const T& x, const T& y) const
{
return x <= y;
}
};
//逻辑类仿函数
template<class T>
struct logical_and : public binary_functione<T, T, bool>
{
bool operator()(const T& x, const T& y) const
{
return x && y;
}
};
template<class T>
struct logical_or : public binary_functione<T, T, bool>
{
bool operator()(const T& x, const T& y) const
{
return x || y;
}
};
template<class T>
struct logical_not : public unary_function<T, bool>
{
bool operator()(const T& x) const
{
return !x;
}
};
//证同函数,任何数通过此函数调用运算后返回原值。
template<class T>
struct identity : public unary_function<T, T>
{
const T& operator()(const T& x) const
{
return x;
}
};
//选择函数 接受pair,传回第一个元素
template<class Pair>
struct select1st : public unary_function<Pair, typename Pair::first_type>
{
const typename Pair::first_type& operator()(const Pair& x) const
{
return x.first;
}
};
template<class Pair>
struct select2nd : public unary_function<Pair, typename Pair::second_type>
{
const typename Pair::second_type& operator()(const Pair& x) const
{
return x.second;
}
};
//投射函数:传回第一参数,忽略第二参数
template<class Arg1, class Arg2>
struct project1st : public binary_functione<Arg1, Arg2, Arg1>
{
Arg1 operator()(const Arg1& x, const Arg2& y) const
{
return x;
}
}
template<class Arg1, class Arg2>
struct project2nd : public binary_functione<Arg1, Arg2, Arg2>
{
Arg2 operator()(const Arg1& x, const Arg2& y) const
{
return y;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫