win7可以做网站吗,设计师之家,网页界面设计代码,手表品牌排行榜文章目录 What什么是#xff1f;How什么时候用#xff1f;如何用#xff1f;bind1st和bind2nd的底层实现原理my_find_if分析myBind1st分析 What什么是#xff1f;
bind1st 和bind2nd分别是一个用来绑定函数对象的第一个参数或第二个参数的适配器。它在 C98 和 C03 标准中很… 文章目录 What什么是How什么时候用如何用bind1st和bind2nd的底层实现原理my_find_if分析myBind1st分析 What什么是
bind1st 和bind2nd分别是一个用来绑定函数对象的第一个参数或第二个参数的适配器。它在 C98 和 C03 标准中很常用但从 C11 开始这个功能已经被认为是过时的deprecated并在后续的 C17 标准中被完全移除。 作用就是 绑定器 二元函数对象 一元函数对象
他俩只能应用于二元函数对象
How什么时候用如何用
下叙函数的代码流程为 定义一个模板函数showContainer来打印任意类型的容器
随机生成一个数组
我们对其进行排序首先使用默认排序less随后使用重载的sort函数丙通过greater从大到小排序其中less和greater都是二元谓词
我们现在有一个需求 把70按顺序插入到vec容器当中 找第一个小于70的数字 很明显我们现在只需要一个一元谓词但是greater和less都是二元谓词这怎么办呢使用绑定器
绑定器的作用就是 绑定器 二元函数对象 一元函数对象
//略去了头文件和命名空间的导入
//...templatetypename Container
void showContainer(Container con) {typename Container::iterator it con.begin();for (; it ! con.end(); it) {cout *it ;}cout endl;
}int main () {vectorint vec;srand(time(nullptr));for (int i 0; i 20; i) {vec.push_back(rand() % 100 1);}showContainer(vec);sort(vec.begin(), vec.end()); //默认递增排序showContainer(vec);//greater是一个二元函数对象,从大到小排序sort(vec.begin(), vec.end(), greaterint());showContainer(vec);/*把70按顺序插入到vec容器当中 找第一个小于70的数字需要一个一元函数对象也就是operator()(const T val)greater a bless a b绑定器 二元函数对象 一元函数对象bind1st: greater bool operator()(70, const _Tp __y) constbind2nd: less bool operator()(const _Tp __x, 70) const*///auto it1 find_if(vec.begin(), vec.end(), //bind1st(greaterint(), 70));auto it1 find_if(vec.begin(), vec.end(), bind2st(lessint(), 70));if (it1 ! vec.end()) {vec.insert(it1, 70);showContainer(vec);}
}bind1st和bind2nd的底层实现原理
我们这里首先实现STL标准库中的算法find_if为my_find_if 然后实现myBind1st main函数结构如下
int main () {vectorint vec;srand(time(nullptr));for (int i 0; i 20; i) {vec.push_back(rand() % 100 1);}showContainer(vec);//greater是一个二元函数对象,从大到小排序sort(vec.begin(), vec.end(), greaterint());showContainer(vec);auto it1 my_find_if(vec.begin(), vec.end(), myBind1st(greaterint(), 70));//auto it1 my_find_if(vec.begin(), vec.end(), //bind2st(lessint(), 70));if (it1 ! vec.end()) {vec.insert(it1, 70);showContainer(vec);}return 0;
}my_find_if分析
从
auto it1 my_find_if(vec.begin(), vec.end(), myBind1st(greaterint(), 70));在STL标准库的find_if可以看到返回类型是一个迭代器类型传参是容器的开始和结束位置的迭代器最后传入的是一个一元谓词。查找的流程就是遍历指定的范围碰到符合条件的即返回。
templatetypename Iterator, typename Compare
Iterator my_find_if(Iterator first, Iterator last, Compare cmp) {for (; first ! last; first) {if (cmp(*first)) { //cmp.operator() (*first)小括号运算符重载return first;}}return last;
} myBind1st分析
首先我们要搞清楚bind1st的传参和返回值
bind1st(greaterint(), 70)传入的是一个二元函数对象和具体的数值返回值其实就是一个一元函数对象所以我们可以模拟出
// myBind1st(greaterint(), 70))
templatetypename Compare, typename T
_mybind1stCompare, T myBind1st (Compare cmp, const T val) {// 直接使用函数模板好处是可以进行类型的推演return _mybind1stCompare, T(cmp, val);
}我们再来实现这个_mybind1st
templatetypename Compare, typename T
class _mybind1st { //绑定器是函数对象的一个应用
public:_mybind1st(Compare cmp, T val):_cmp(cmp), _val(val){}bool operator() (const T second) { //重载函数调用运算符return _cmp(_val, second);}
private:Compare _cmp;T _val;
};这样我们就可以愉快得使用啦