STL算法设计理念 - 二元函数,二元谓词以及在set中的应用
demo 二元函数对象
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
template <typename T>
class SumVector
{
public:
T operator()(T t1, T t2) // 二元函数对象
{
return t1 + t2;
}
protected:
private:
};
void play01()
{
vector<int> v1, v2, v3;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
v2.push_back(2);
v2.push_back(4);
v2.push_back(6);
v3.resize(10);
transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), SumVector<int>());
/* transform函数原型
template<class _InIt1,
class _InIt2,
class _OutIt,
class _Fn2> inline
_OutIt transform(_InIt1 _First1, _InIt1 _Last1,
_InIt2 _First2, _OutIt _Dest, _Fn2 _Func)
{ // transform [_First1, _Last1) and [_First2, ...) with _Func
_DEBUG_RANGE(_First1, _Last1);
_DEBUG_POINTER(_Dest);
_DEBUG_POINTER(_Func);
if (_First1 != _Last1)
return (_Transform2(_Unchecked(_First1), _Unchecked(_Last1),
_First2, _Dest, _Func,
_Is_checked(_Dest)));
return (_Dest);
}
*/
// transform把运算结果迭代器的开始位置返回出来
for (vector<int>::iterator it = v3.begin(); it != v3.end(); ++it) {
cout << *it << ' ';
}
cout << endl;
// 3 7 11 0 0 0 0 0 0 0
}
int main()
{
play01();
return 0;
}
demo 二元谓词
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;
void printVector(vector<int> &v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
cout << *it << ' ';
}
cout << endl;
}
void FuncShowElemt2(const int &t)
{
cout << t << ' ';
}
// 二元谓词
bool myCompare(const int &a, const int &b)
{
return a < b;
}
void play01()
{
vector<int> v(10);
srand(time(0));
for (int i = 0; i < 10; i++) {
int tmp = rand() % 100;
v[i] = tmp;
}
printVector(v);
// 90 19 94 50 90 90 24 50 30 74
for_each(v.begin(), v.end(), FuncShowElemt2);
cout << endl;
// 90 19 94 50 90 90 24 50 30 74
sort(v.begin(), v.end(), myCompare);
for_each(v.begin(), v.end(), FuncShowElemt2);
cout << endl;
// 0 8 14 23 32 33 44 45 63 80
}
int main()
{
play01();
return 0;
}
demo 二元谓词在set中的应用
#include <iostream>
#include <cstdio>
#include <set>
#include <algorithm>
#include <string>
using namespace std;
struct CompareNoCase
{
bool operator()(const string &str1, const string &str2)
{
string tmpstr1;
tmpstr1.resize(str1.size());
transform(str1.begin(), str1.end(), tmpstr1.begin(), tolower);
string tmpstr2;
tmpstr2.resize(str2.size());
transform(str2.begin(), str2.end(), tmpstr2.begin(), tolower);
return tmpstr1 < tmpstr2;
}
};
void play01()
{
set<string> set1;
set1.insert("lucifer");
set1.insert("zhang");
set1.insert("yaoqi");
set<string>::iterator it1 = set1.find("LUcifer"); // find函数默认区分大小写
if (it1 == set1.end()) {
cout << "find fail\n";
}
else {
cout << "find success\n";
}
// find fail
set<string, CompareNoCase> set2;
set2.insert("lucifer");
set2.insert("zhang");
set2.insert("yaoqi");
set<string, CompareNoCase>::iterator it2 = set2.find("LUcifer"); // find函数默认区分大小写
if (it2 == set2.end()) {
cout << "find fail\n";
}
else {
cout << "find success\n";
}
// find success
}
int main()
{
play01();
return 0;
}
STL算法设计理念 - 二元函数,二元谓词以及在set中的应用的更多相关文章
- STL算法设计理念 - 函数适配器
1)函数适配器的理论知识 2)常用函数函数适配器 标准库提供一组函数适配器,用来特殊化或者扩展一元和二元函数对象.常用适配器是: 1.绑定器(binder): binder通过把二元函数对象的一个实参 ...
- STL算法设计理念 - 谓词,一元谓词demo
谓词: 一元函数对象:函数参数1个: 二元函数对象:函数参数2个: 一元谓词 函数参数1个,函数返回值是bool类型,可以作为一个判断式 谓词可以使一个仿函数,也可以是一个回调函数. demo 一元谓 ...
- STL算法设计理念 - 函数对象和函数对象当参数和返回值
函数对象: 重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象.一个类对象,表现出一个函数的特征,就是通过"对象名+(参数列表)&qu ...
- STL算法设计理念 - 函数对象和函数对象当參数和返回值
函数对象: 重载函数调用操作符的类.其对象常称为函数对象(function object),即它们是行为类似函数的对象. 一个类对象,表现出一个函数的特征,就是通过"对象名+(參数列表)&q ...
- STL算法设计理念 - 预定义函数对象
预定义函数对象基本概念:标准模板库STL提前定义了很多预定义函数对象 1)使用预定义函数对象: #include <iostream> #include <cstdio> #i ...
- C++ STL算法系列1---count函数
一.count函数 algorithm头文件定义了一个count的函数,其功能类似于find.这个函数使用一对迭代器和一个值做参数,返回这个值出现次数的统计结果. 编写程序读取一系列int型数据,并将 ...
- C++ STL算法系列6---copy函数
现在我们来看看变易算法.所谓变易算法(Mutating algorithms)就是一组能够修改容器元素数据的模板函数,可进行序列数据的复制,变换等. 我们现在来看看第一个变易算法:元素复制算法copy ...
- 【转】三十分钟学会STL算法
转载自: http://net.pku.edu.cn/~yhf/UsingSTL.htm 这是本小人书.原名是<using stl>,不知道是谁写的.不过我倒觉得很有趣,所以化了两个晚上把 ...
- C++进阶 STL(2) 第二天 一元/二元函数对象、一元/二元谓词、stack容器、queue容器、list容器(双向链表)、set容器、对组、map容器
01 上次课程回顾 昨天讲了三个容器 string string是对char*进行的封装 vector 单口容器 动态数组 deque(双端队列) 函数对象/谓词: 一元函数对象: for_each ...
随机推荐
- HDFS基本原理及数据存取实战
---------------------------------------------------------------------------------------------------- ...
- Mac状态栏wifi图标一直闪烁重复连接但是网络正常的解决办法
本猫的系统是EI(10.11.6),不知从哪个版本开始(至少是升级到EI之后),状态栏上的wifi图标一直闪烁,这应该是表示正在连接网络.但是网络是正常的! 虽说闪烁的wifi图标不影响使用,但是有强 ...
- OpenResty修改Nginx默认autoindex页面
Nginx的autoindex 命令可以自动列出目录下的文件,一些网站用这个功能做文件下载,但是Nginx又没有提供这个页面的 自定义的功能,后来看到别人提及 ngx_openresty,才想到 bo ...
- PHP+MySQL 分页那点事
分页技术随处可见,这可以算得上是最为基础的网站功能了.于是今天尝试着用PHP来实现一个分页的小例子. 准备工作 环境准备 Apache MySQL PHP 工作环境 数据库准备 建库 建表 预存数据 ...
- apply函数用法
procedure: (apply proc arg1 ... args) Proc must be a procedure and args must be a list. Calls proc ...
- Android fragment(片段)构建灵活的UI
在以支持多种屏幕尺寸为目标设计应用时,您可以在不同的布局配置中重复使用您的fragment 从而根据可用的屏幕空间优化用户体验. 例如,在手机设备上,由于采用单窗格用户界面,因此可能更适合一次只显示一 ...
- android 集成微博常见问题
我们在做微博集成登录.分享.聊天的时候,肯定会遇到很多的坑,这里总结下常见的问题. 文件不存在 C8998 的解决方法 如图我们走微博授权登录的时候如果OAuth2.0 授权设置回调页面设置和本地的不 ...
- Android中PropertyAnimation属性动画详解(一)
在之前的文章中已经讲了帧动画frame-by-frame animation和补间动画tweened animation,其实这两种动画原理好简单,都是按照预先固定的动画模式来播放的,帧动画将一张张单 ...
- Android反编译 -- 错误代码还原
PS:如果阅读体验不好,可以尝试Github版 (<-点左边) 1. setColor(-16777216) 反编译的代码中会有很多setColor(int)的情况,比如setColor(-16 ...
- android RecycleView Adapter简单封装
早些时候我们使用系统提供个的BaseAdapter的时候为了满足大家的需要,我们总会对BaseAdapter做一层上层的封装,然后对于实际业务我们只需要关心getView里面的View即可,是代码可读 ...