10.3.4参数绑定 bind
Count_if算法,类似find_if,此函数接受一对迭代器,表示一个输入范围,还接受一个谓词,会对输入范围中的每个元素执行。Count_if返回一个计数值,表示谓词有多少次为真。
使用bind函数必须包含头文件functional且必须包含命名空间placeholders,该命名空间也包含于functional头文件中,所以使用此命名空间也必须包含此头文件, 如:
using namespace (std::)placeholders;
或 using (std::)placeholders::_1; //名字_n都包含于命名空间placeholders
vector<string>::iterator iter = find_if(words.begin(), words.end(),
bind(check_size, _1, sz));
bind的参数个数:
bind是可变参数的,他接受的第一个参数是一个可调用对象,级实际工作函数A,返回供算法使用的新的可调用对象B。若A接受x个参数,则bind的参数个数应该是x+1,即除了A外,其他参数应一一对应A所接受的参数。这些参数中有一部分来自B(_n),另外一些来自于所处函数的局部变量。
下面是一个使用bind的例子
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
using namespace placeholders;
ostream &print(ostream &os, const string &s, char c);
//using placeholders::_1;
inline void output(vector<string> &words)
{
for_each(words.begin(), words.end(), bind(print, ref(cout), _1, ' '));
cout << endl;
}
inline bool isShort(const string &a, const string &b)
{
return a.size() < b.size();
}
ostream &print(ostream &os, const string &s, char c)
{
return os << s << c;
}
inline bool check_size(const string &s, string::size_type sz)
{
return s.size() >= sz;
}
void biggies(vector<string> &words, vector<string>::size_type sz)
{
sort(words.begin(), words.end());
output(words);
auto it = unique(words.begin(), words.end());
output(words);
words.erase(it, words.end());
output(words);
stable_sort(words.begin(), words.end(),
bind(isShort, _1, _2));
output(words);
vector<string>::iterator iter = stable_partition(words.begin(), words.end(),
bind(check_size, _1, sz));
output(words);
int count = iter - words.begin();
cout << "There is " << count << (count > 1 ? " words" : " word")
<< " is 5 or longer." << endl;
for_each(words.begin(), iter, [](const string &s) { cout << s << " "; });
cout << endl;
}
int main()
{
vector<string> words{ "the", "quick", "red", "fox", "jumps",
"over", "the", "slow", "red", "turtle" };
biggies(words, 5);
return 0;
}
10.3.4参数绑定 bind的更多相关文章
- thinkphp 参数绑定
参数绑定是指绑定一个参数到预处理的SQL语句中的对应命名占位符或问号占位符指定的变量,并且可以提高SQL处理的效率,需要数据库驱动类的支持,目前只有PDO和Sqlsrv驱动支持参数绑定功能. 富瑞华大 ...
- JAVAEE——SpringMVC第一天:介绍、入门程序、架构讲解、SpringMVC整合MyBatis、参数绑定、SpringMVC和Struts2的区别
1. 学习计划 第一天 1.SpringMVC介绍 2.入门程序 3.SpringMVC架构讲解 a) 框架结构 b) 组件说明 4.SpringMVC整合MyBatis 5.参数绑定 a) Sp ...
- SpringMVC介绍及参数绑定
本节内容: SpringMVC介绍 入门程序 SpringMVC架构 SpringMVC整合MyBatis 参数绑定 SpringMVC和Struts2的区别 一.SpringMVC介绍 1. 什么是 ...
- springMVC第一天——入门、整合与参数绑定
大纲摘要: 1.Springmvc介绍 2.入门程序 3.Springmvc架构讲解 a) 框架结构 b) 组件说明 4.Springmvc整合mybatis 5.参数绑定 乱码问题解决 a) Spr ...
- SpringMVC的参数绑定
一.@RequestMapping注解说明 通过@RequestMapping注解可以定义不同的处理器映射规则. URL路径映射 @RequestMapping(value="/item ...
- Python中函数参数类型和参数绑定
参数类型 Python函数的参数类型一共有五种,分别是: POSITIONAL_OR_KEYWORD(位置参数或关键字参数) VAR_POSITIONAL(可变参数) KEYWORD_ONLY(关键字 ...
- SpringMVC入门 -- 参数绑定
一.REST与RESTful 1.简介 (1)REST(Representational State Transfer):表现层状态转移,一种软件架构风格,不是标准.REST描述的是在网络中clien ...
- 【工作篇】再次熟悉 SpringMVC 参数绑定
前言 主要现在项目中使用的参数绑定五花八门的,搞得很头大,例如有些用字符串接收日期,用字符串接受数组等等,完全没有利用好 SpringMVC 的优势,这里自己也总结一下,免得到时又要百度谷歌查找. 以 ...
- Spring MVC初始化参数绑定
初始化参数绑定与类型转换很类似,初始化绑定时,主要是参数类型 ---单日期 在处理器类中配置绑定方法 使用@InitBinder注解 在这里首先注册一个用户编辑器 参数一为目标类型 proper ...
随机推荐
- jdk1.7、jdk1.8兼容共存环境配置方案
前提:环境变量配置 JAVA_HOME=%JAVA_HOME8% JAVA_HOME8=E:\jdk1.8.0_40 JAVA_HOME7=E:\jdk1.7.0_51 path 一.jdk是绿色免安 ...
- 将Chrome浏览器中的扩展程序导出为crx插件文件
将Chrome浏览器中安装的插件程序导出为crx插件文件 以360急速浏览器为例进行导出crx插件程序 1.在Chrom商店中找到需要的插件,安装到浏览器的扩展程序里面()IDM Integratio ...
- 第7章.字符串、String类和StringBuilder类
参考链接: https://www.runoob.com/csharp/csharp-string.html https://www.cnblogs.com/cang12138/p/7323709.h ...
- 剑指offer-面试题53_3-数组中数值和下标相等的元素-二分查找
/* 题目: 求单调递增数组中,数值与下标相等的任意数字. */ /* 思路: 二分法. */ #include<iostream> #include<cstring> #in ...
- Vulnerable Kerbals CodeForces - 772C【拓展欧几里得建图+DAG上求最长路】
根据拓展欧几里得对于同余方程 $ax+by=c$ ,有解的条件是 $(a,b)|c$. 那么对于构造的序列的数,前一个数 $a$ 和后一个数 $b$ ,应该满足 $a*x=b(mod m)$ 即 $ ...
- Mybatis的延迟加载和立即加载
Mybatis的延迟加载和立即加载 示例:在一对多中,当我们有一个用户,他有100个帐户 问题1:在查询用户时,要不要把关联的账户查出来? 问题2:在查询账户时,要不要把关联的用户信息查出来? 问题1 ...
- hibernate报错:MappingException: Could not determine type for...解决办法
有时候实体里的一些属性并不想映射到数据库(比方说子级菜单List), 如果不做处理的话会报字段映射错误找不到这列Column Not Found 例如:org.hibernate.MappingExc ...
- JavaSE学习笔记(3)---面向对象三大特性
JavaSE学习笔记(3)---面向对象三大特性 面向对象的三大特征:继承.封装.多态 1.封装 面向对象编程语言是对客观世界的模拟,客观世界里成员变量都是隐藏在对象内部的,外界无法直接操作和修改.然 ...
- 使用opencv自带Tracker进行目标跟踪——重新设定跟踪目标
当希望重新设定一个目标进行跟踪的时候,以下两种做法都是无效的: 1.将新对象的Rect2d直接传递给update()函数: 2.再次使用tracker的init()函数. 解决办法:重新创建一个Tra ...
- select count(1)和select count(*)的区别
select count(1) from 表a //查询时会对常数列进行统计行数select count(*) from 表a //查询时会找表a中最短的列进行统计行数 因为使用count(*)查询会 ...