not1,not2,bind1st,bind2nd
例子需要包含头文件
#include <vector>
#include <algorithm>
#include <functional>
bind1st和bind2nd函数用于将一个二元函数对象(binary functor,bf)转换成一元函数对象(unary functor,uf)。为了达到这个目的,它们需要两个参数:要转换的bf和一个值(v)。
可能这么解释以后大家还不是很清楚,那么就说点白话吧。我们在做比较的时候所写的表达式像 x > k ,x < k,这里的k是一个参数表示你程序里面的表达式要和k值去比较。上面这两个表达式对应的应该是bind2nd ,简单的理解就是把k作为比较表达式的第二个参数。如果使用bind1st则对应的表达式是 k > x,k < x,也就是把k作为比较表达式的第一个参数。大家可能会注意到这里面没有=的比较,先别着急,后面将会说道如何实现=的比较。先举两个例子看看bind1st和bind2nd的用法。
- f = std::bind1st( functor, v); 'f( x)'等价于'functor( v, x)'
- f = std::bind2nd( functor, v); 'f( x)'等价于'functor( x, v)'
int a[] = {1, 2, 100, 200};std::vector< int> arr(a, a + 4);// 移除所有小于100的元素 arr.erase( std::remove_if( arr.begin(), arr.end(),std::bind2nd( std::less< int>(), 100)), arr.end());
这里的比较表达式相当于arr.value < 100,如果用bind1st则表达的意思就恰恰相反
// 移除所有大于100的元素 arr.erase( std::remove_if( arr.begin(), arr.end(),std::bind1st( std::less< int>(), 100)), arr.end());
这里的表达式相当于100 < arr.value,然为了实现删除大于100的元素你同样可以使用bind2nd
// 移除所有大于100的元素 arr.erase( std::remove_if( arr.begin(), arr.end(),std::bind2nd( std::greater< int>(), 100)), arr.end());
前面说道=的比较,比如说x <= k怎么实现呢,std又提供了一个好东西not1,我们可以说 !(x > k) 和 x <= k是等价的,那么我们看看下面的表达式:
// 移除所有小于等于100的元素 arr.erase( std::remove_if( arr.begin(), arr.end(),std::not1(std::bind2nd( std::greater< int>(), 100))), arr.end());
not1是构造一个与谓词结果相反的一元函数对象,not2是构造一个与谓词结果相反的二元函数对象。
// not1 example #include <iostream> // std::cout #include <functional> // std::not1 #include <algorithm> // std::count_ifstruct IsOdd {bool operator() (const int& x) const {return x%2==1;}typedef int argument_type; };int main () {int values[] = {1,2,3,4,5};int cx = std::count_if (values, values+5, std::not1(IsOdd()));std::cout << "There are " << cx << " elements with even values.\n";return 0; }
1 // not2 example 2 #include <iostream> // std::cout 3 #include <functional> // std::not2, std::equal_to 4 #include <algorithm> // std::mismatch 5 #include <utility> // std::pair 6 7 int main () { 8 int foo[] = {10,20,30,40,50}; 9 int bar[] = {0,15,30,45,60}; 10 std::pair<int*,int*> firstmatch,firstmismatch; 11 firstmismatch = std::mismatch (foo, foo+5, bar, std::equal_to<int>()); 12 firstmatch = std::mismatch (foo, foo+5, bar, std::not2(std::equal_to<int>())); 13 std::cout << "First mismatch in bar is " << *firstmismatch.second << '\n'; 14 std::cout << "First match in bar is " << *firstmatch.second << '\n'; 15 return 0; 16 }
转载于:https://www.cnblogs.com/13224ACMer/p/6388141.html
not1,not2,bind1st,bind2nd相关推荐
- bind1st bind2nd的使用
bind1st bind2nd的使用 Posted on 2010-07-16 09:14 wanpengcoder 阅读(1882) 评论(0) 编辑 收藏 bind1st和bind2nd函数用于将 ...
- 关于标准库中的ptr_fun/binary_function/bind1st/bind2nd
http://www.cnblogs.com/shootingstars/archive/2008/11/14/860042.html 以前使用bind1st以及bind2nd很少,后来发现这两个函数 ...
- 《EffcativeSTL》
定义.使用和扩展STL 没有"STL"的官方定义,在本书中,"STL"的意思是与迭代器合作的C++标准库的一部 分.那包括标准容器(包括string),iost ...
- 《EfficativeSTL》
定义.使用和扩展STL 没有"STL"的官方定义,在本书中,"STL"的意思是与迭代器合作的C++标准库的一部 分.那包括标准容器(包括string),iost ...
- Effictive STL读书笔记
都是一些简单的总结,可以帮助回忆当时看书的知识点~ ##第1条:慎重选择容器类型 STL容器的分类远比我想像中的多.别人意外的是stack,queue等不是STL容器,但这不是这章的重点. 你是否需要 ...
- C++for_each| bind1st | ptr_fun | std::function的用法
c++ for_each 用法_小键233-CSDN博客 传入参数 要传入参数给global function ,需要使用 ptr_fun() 这个 function adapter 将global ...
- STL17-函数对象
仿函数: #include<iostream> #include<vector> #include<algorithm> using namespace std; ...
- [转]【C/C++】STL详解
转载备用,原创作者在文章结尾... 文章目录 概述 STL六大组件简介 三大组件介绍 1. 容器 2. 算法 3. 迭代器 常用容器 1. string容器 string容器基本概念 string ...
- 【C/C++学习】之STL详解
文章目录 概述 STL六大组件简介 三大组件介绍 容器 算法 迭代器 常用容器 1. string容器 string容器基本概念 string容器常用操作 2. vector容器 ...
- c++实现STL标准库
本次课程主要面对有一定 c++ 基础(了解基本语法,熟悉常用特性)的 ,想要学习 c++ 更深入特性 ,掌握 c++ 强大标准库的同学 .通过本次课程,你将学习到 c++ template ,异常处理 ...
最新文章
- 深度概览卷积神经网络全景图,没有比这更全的了
- linux搭建gitlab
- 100小时学会sap-财务篇fico总结介绍篇
- bat脚本交互输入_测评 | 不使用powershell运行 PowerShell 脚本的工具汇总
- mysql索引如何做_5分钟,告诉你MySQL字符串怎么做索引
- 广外计算机考研专业课,【广外考研论坛】 21广外各专业考研问题全解答!纯干货!...
- UI5 control inheritance implementation question
- 21、mysql修改密码的方法总结
- JBoss AS 7:自定义登录模块
- mysql sharding 读取_MySQL读写分离(一)——sharding-jdbc
- 建议考事业编吗?为什么?
- php中函数的类型提示和文件读取功能
- pkgm : 压缩包维护与解压脚本
- Citrix XenApp6.5 另类发布文档
- 方舟生存 服务器修改器,【修改贴】关于单机版gg修改器的应用。
- 《程序员拒绝一个合理需求的15个方法!》
- 第四章第六题(圆上的随机点)(Random points on a circle)
- source insight中文乱码
- 第三十四章 苏西受伤
- js 禁用输入法(伪) / keydown返回false仍然可以输入的问题
热门文章
- 如何在 Mac 上设置 FaceTime?
- Blocs 4 for Mac(可视化网页设计工具)
- Macs Fan Control Pro for mac(电脑风扇控制软件)v1.5.10中文
- 如何通过Multitouch为Mac电脑添加更多手势控制?
- eclipse修改xml文件默认的打开方式为XML Editor
- 拇指接龙游戏从WIN32向Android移植过程问题记录(1)
- Java第十二次作业:什么是一维数组?什么是对象数组?吃金币游戏2.0版 新增炸弹功能 新增游戏倒计时功能 新增胜利失败检测功能 使用如鹏游戏引擎制作窗体 一维数组设置金币...
- Python爬虫(一)抓取指定的页面
- Wordpress 错误ERROR: Cookies are blocked or not supported by your browser.
- mac查看进程 总是忘记