#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; // transform 将一个容器的元素 搬运 到另一个容器中
struct MyPlus
{
int operator()(int val)
{
return val + ;
}
}; void MyPrint(int val)
{
cout << val << " ";
} void test01()
{
vector<int> v1;
vector<int> v2;
for (int i = ; i < ; i++)
{
v1.push_back(i);
}
v2.resize(v1.size()); // 开辟空间
transform(v1.begin(), v1.end(), v2.begin(), MyPlus());
for_each(v2.begin(), v2.end(), MyPrint);
cout << endl;
cout << "--------------------" << endl;
} // 常用的查找算法
void test02()
{
vector<int> v1;
for (int i = ; i < ; i++)
{
v1.push_back(i);
}
vector<int>::iterator ret = find(v1.begin(), v1.end(), );
if (ret == v1.end())
{
cout << "没有找到!" << endl;
}
else
{
cout << "找到了:" << *ret << endl;
}
cout << "--------------------" << endl;
} class Person
{
public:
Person(int age, int id) : age(age), id(id){}
bool operator== (const Person& p)
{
return p.id == this->id && p.age == this->age;
}
public:
int id;
int age;
}; void test03()
{
vector<Person> v1;
Person p1(, ), p2(, );
v1.push_back(p1);
v1.push_back(p2);
vector<Person>::iterator ret = find(v1.begin(), v1.end(), p1);
if (ret == v1.end())
{
cout << "没有找到!" << endl;
}
else
{
cout << "找到了!" << endl;
}
cout << "--------------------" << endl;
} bool MySearch(int val)
{
return val > ;
} bool MySearch2(int val)
{
return val > ;
} void test04()
{
vector<int> v1;
for (int i = ; i < ; i++)
{
v1.push_back(i);
}
v1.push_back();
bool ret = binary_search(v1.begin(), v1.end(), );
if (ret)
{
cout << "找到了!" << endl;
}
else
{
cout << "没有找到!" << endl;
} vector<int>::iterator it = adjacent_find(v1.begin(), v1.end());
if (it != v1.end())
{
cout << "找到相邻重复元素:" << *it << endl;
}
else
{
cout << "没有找到相邻重复元素!" << endl;
} // find_f 会根据我们的条件(函数) ,返回第一个满足条件的元素的迭代器
it = find_if(v1.begin(), v1.end(), MySearch);
if (it != v1.end())
{
cout << "找到:" << *it << endl;
}
else
{
cout << "没有找到!" << endl;
} // count count_if
int num = count(v1.begin(), v1.end(), );
cout << "9出现的次数:" << num << endl;
num = count_if(v1.begin(), v1.end(), MySearch2);
cout << "大于5的个数:" << num << endl;
} int main()
{
test01();
test02();
test03();
test04();
getchar();
return ;
}

C++ STL 之 常用算法的更多相关文章

  1. STL中常用算法

    一.排序 sort sort(first_pointer,first_pointer+n,cmp) 默认为升序 若要使用降序,自行写cmp 函数 bool cmp(int a,int b){ retu ...

  2. STL——配接器、常用算法使用

    学习STL,必然会用到它里面的适配器和一些常用的算法.它们都是STL中的重要组成部分. 适配器 在STL里可以用一些容器适配得到适配器.例如其中的stack和queue就是由双端队列deque容器适配 ...

  3. [C++ STL] 常用算法总结

    1 概述 STL算法部分主要由头文件<algorithm>,<numeric>,<functional>组成.要使用 STL中的算法函数必须包含头文件<alg ...

  4. ACM常用算法及练习(2)

    ACM常用算法及练习 知识类型 重要度 容易度 应掌握度 典型题 其他           数据结构(5) 链表 ★★☆ ★★★ ★★☆     栈 stack ★★★ ★★★ ★★★ HLoj120 ...

  5. ACM常用算法及练习(1)

    ACM常用算法及练习 第一阶段:练经典常用算法,下面的每个算法给我打上十到二十遍,同时自己精简代码,因为太常用,所以要练到写时不用想,10-15分钟内打完,甚至关掉显示器都可以把程序打出来. 1.最短 ...

  6. 总结Objective-c常用算法

          今天是星期天,想睡到10点起床,结果认为自己太奢侈了,不能这么做,于是把闹钟设置成了6:30:结果终于9:36醒了,起床,无缘无故迟了,好吧,就算太累了吧,周天就原谅自己一回.终于到了中午 ...

  7. Atitit 编程语言常用算法attilax总结

    Atitit 编程语言常用算法attilax总结 1. 编译算法分类and   数据操作算法.1 1.1. Tab driver stat  状态转换表格算法1 1.2. Nest case 词法分析 ...

  8. GJM : 数据结构 - 轻松看懂机器学习十大常用算法 [转载]

     转载请联系原文作者 需要获得授权,非法转载 原文作者将享受侵权诉讼 文/不会停的蜗牛(简书作者)原文链接:http://www.jianshu.com/p/55a67c12d3e9 通过本篇文章可以 ...

  9. C/C++常用算法【C语言顺序查找(随机数)】【1】

    这是我学习唐峻,李淳的<C/C++常用算法第一天> 1.8.1. 查找数字: 程序随机生成一个拥有20个整数数据的数组,然后输入要查找的数据.接着,可以采用醉简单的逐个对比的方法进行查找, ...

随机推荐

  1. osg::GraphicsContext::WindowingSystemInterface Screen Resolution(屏幕分辨率)

    unsigned int width, height; //获取系统分辨率 osg::GraphicsContext::WindowingSystemInterface *wsInterface = ...

  2. QML显示圆形图片

    Item {//一个圆形图片 width: parent.width height: parent.height Image { id: rdJpg anchors.centerIn: parent ...

  3. mysql的root用户无法给普通用户授权问题处理

    1.查看Grant_priv是Y还是N 执行下面 select * from mysql.user where User='root' and Host='%'\G; 下图查看结果为Grant_pri ...

  4. Node.js ORM框架Sequelize使用示例

    示例代码: const Sequelize = require('sequelize'); const sequelize = new Sequelize('database', 'username' ...

  5. 2019年12月4日 Linux总结

    System V init运行级别 systemd目标名称 作用 0 runlevel0.target,poweroff.target 关机 1 runlevel1.target,poweroff.t ...

  6. 【VS开发】Wix 安装教程

    original link :  http://www.cnblogs.com/stoneniqiu/p/3355086.html 因为项目需要,最近在研究Wix打包部署,园子里也有一些关于wix ...

  7. Linux与windows的对比

    Linux与windows的对比 声明:下面的内容总结自实验楼的linux教程,特在此说明. Linux与windows一样,是一个操作系统. Linux与Windows的区别 软件与支持 windo ...

  8. 安卓计数器类APP推荐

    1. Thing Counter - Google Play 上的应用 可添加多个计数器,同一页面展示,一行一个,操作直观方便: 可以更改计数器颜色,使页面更美观. 每个计数器有详情和历史记录 详情: ...

  9. centos(6,7) 系统常用命令

    目录: 系统服务命令 文件操作 系统信息 文件和目录 文件搜索 挂载一个文件系统 磁盘空间 用户和群组 文件的权限 压缩与解压缩 YUM丶RPM 包 查看文件内容 文本处理 文件系统分析 初始化一个文 ...

  10. Oracle 表的导入与导出

    周末任务:将一个表从一个库导到另一个库大致思路:用expdp 将表从一个实例导出 ,再用impdp将导出的 .dmp文件导入到另一个实例1.在实例 orcl 中准备一个用于导出的表: 进入实例为orc ...