#include <algorithm>
1 adjacent_find
查找重复的元素
2 find_if
查找符合条件的第一个元素
3 find_if_not
查找不符合条件的第一个元素
4 for_each
可以遍历每一个元素
5 partial_sort
部分排序
6 partition
服务于快速排序法的分区
7 prev_permutation
排序
8 random_shuffle
随机排序
9 rotate
旋转
adjacent_find
查找重复的元素
#include <iostream>
#include <algorithm>
#include <set> void main()
{
std::multiset<int>myset; myset.insert();
myset.insert();
myset.insert();
myset.insert();
myset.insert(); auto it = adjacent_find(myset.begin(), myset.end()); std::cout << *it << std::endl; it = adjacent_find(it++, myset.end()); std::cout << *it << std::endl; it = adjacent_find(it++, myset.end()); std::cout << *it << std::endl;
}
find
1 #include <iostream>
2 #include <algorithm>
3 #include <vector>
4
5 void main()
6 {
7 std::vector<int>myv;
8
9 myv.push_back(1);
10 myv.push_back(2);
11 myv.push_back(3);
12
13 auto i = find_if(myv.begin(), myv.end(), [](int v)->bool {return v < 4; });//返回第一个小于4的元素
14
15 if (i == myv.end())
16 {
17 std::cout << "not found" << std::endl;
18 }
19 else
20 {
21 std::cout << *i << std::endl;
22 }
23 }
find_if
查找符合条件的第一个元素
1 #include <iostream>
2 #include <algorithm>
3 #include <vector>
4
5 void main()
6 {
7 std::vector<int>myv;
8
9 myv.push_back(1);
10 myv.push_back(2);
11 myv.push_back(3);
12
13 auto i = find_if(myv.begin(), myv.end(), [](int v)->bool {return v < 4; });//返回第一个小于4的元素
14
15 if (i == myv.end())
16 {
17 std::cout << "not found" << std::endl;
18 }
19 else
20 {
21 std::cout << *i << std::endl;
22 }
23 }
find_if_not
查找不符合条件的第一个元素
1 #include <iostream>
2 #include <algorithm>
3 #include <vector>
4
5 void main()
6 {
7 std::vector<int>myv;
8
9 myv.push_back(1);
10 myv.push_back(2);
11 myv.push_back(3);
12
13 auto i = find_if_not(myv.begin(), myv.end(), [](int v)->bool {return v < 4; });//返回第一个不是小于4的元素
14
15 if (i == myv.end())
16 {
17 std::cout << "not found" << std::endl;
18 }
19 else
20 {
21 std::cout << *i << std::endl;
22 }
23 }
//[地址](参数) {语句; }
//&res直接操作一个变量,res等价于返回值,x代表参数,每次充当迭代器指向的元素
//不仅仅适用于array,也适用于vector
vector使用for_each
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; void main()
{
std::vector<int>myvector;//创建一个数组,数组元素是int类型 myvector.push_back();//尾部插入
myvector.push_back();
myvector.push_back(); int res = ;//保存结果 //[地址](参数) {语句; }
//&res直接操作一个变量,res等价于返回值,x代表参数,每次充当迭代器指向的元素
//不仅仅适用于array,也适用于vector
for_each(myvector.begin(), myvector.end(), [&res](int x) {res += x; }); std::cout << res;//打印 system("pause");
}
普通数组使用for_each
#include <iostream>
#include <algorithm> struct print
{
void operator ()(int x)//重载()
{
std::cout << x << std::endl;
}
}; void printA(int x)
{
std::cout << x << std::endl;
} int main()
{
int a[] = { ,,,,,,,,, };
int *p = std::find(a, a + , ); std::cout << a << " " << a + << std::endl;
std::cout << *p << std::endl; if (p == a + )
{
printf("没有找到\n");
} std::for_each(a, a + , print());//第三个参数是函数指针,必须是函数类型
std::for_each(a, a + , printA); return ;
}
5 partial_sort
部分排序
#include <iostream>
#include <algorithm>
#include <string>
#include <vector> struct student
{
public:
std::string name;
int score;
public:
student(std::string str, int num) :name(str), score(num)
{ }
bool operator<(const student &s1)const
{
return this->score < s1.score;
}
}; void main()
{
std::vector<student>ss; {
student s1("AA", );
ss.push_back(s1);
} {
student s1("BB", );
ss.push_back(s1);
} {
student s1("CC", );
ss.push_back(s1);
} {
student s1("DD", );
ss.push_back(s1);
} partial_sort(ss.begin(), ss.begin() + , ss.end()); for (int i = ; i < ; i++)
{
std::cout << ss[i].name << " " << ss[i].score << std::endl;
}
}
partition
服务于快速排序法的分区
#include <iostream>
#include <algorithm>
#include <vector> template <class T>
struct show
{
public:
void operator()(T &t)
{
std::cout << t << " ";
}
}; bool isok(int num)
{
return num == ;
} void main()
{
std::vector<int>myv; for (int i = ; i < ; i++)
{
myv.push_back(i);
} for_each(myv.begin(), myv.end(), show<int>());
std::cout << std::endl; partition(myv.begin(), myv.end(), isok); for_each(myv.begin(), myv.end(), show<int>());
std::cout << std::endl;
}
prev_permutation
排序
#include <iostream>
#include <algorithm> void main()
{
int a[] = { ,,, }; do
{
std::cout << a[] << " " << a[] << " " << a[] << " " << a[] << std::endl;
} while (std::prev_permutation(a, a + ));
}
random_shuffle
随机排序
#include <iostream>
#include <algorithm>
#include <vector> template <class T>
struct show
{
public:
void operator()(T &t)
{
std::cout << t << " ";
}
}; void main()
{
std::vector<int>myv; for (int i = ; i < ; i++)
{
myv.push_back(i);
} for_each(myv.begin(), myv.end(), show<int>());
std::cout << std::endl; random_shuffle(myv.begin(), myv.end());//随机排序 for_each(myv.begin(), myv.end(), show<int>());
std::cout << std::endl; random_shuffle(myv.begin(), myv.end());//随机排序 for_each(myv.begin(), myv.end(), show<int>());
std::cout << std::endl;
}
rotate
旋转
#include <iostream>
#include <algorithm>
#include <vector> template <class T>
struct show
{
public:
void operator()(T &t)
{
std::cout << t << " ";
}
}; bool isok(int num)
{
return num == ;
} void main()
{
std::vector<int>myv; for (int i = ; i < ; i++)
{
myv.push_back(i);
} for_each(myv.begin(), myv.end(), show<int>());
std::cout << std::endl; rotate(myv.begin(), myv.begin() + , myv.end()); for_each(myv.begin(), myv.end(), show<int>());
std::cout << std::endl; rotate(myv.begin(), myv.begin() + , myv.end()); for_each(myv.begin(), myv.end(), show<int>());
std::cout << std::endl;
}
#include <algorithm>的更多相关文章
- 浅谈JSP中include指令与include动作标识的区别
JSP中主要包含三大指令,分别是page,include,taglib.本篇主要提及include指令. include指令使用格式:<%@ include file="文件的绝对路径 ...
- Entity Framework 6 Recipes 2nd Edition(13-9)译 -> 避免Include
问题 你想不用Include()方法,立即加载一下相关的集合,并想通过EF的CodeFirst方式实现. 解决方案 假设你有一个如Figure 13-14所示的模型: Figure 13-14. A ...
- error RC1015: cannot open include file 'afxres.h' 解决办法
在为WindowsPhone8程序添加本地化的过程中遇到这个问题: 问题原因就是afxres.h文件缺失,下载它,放到VS安装目录下的VS\include目录下就可以了(选择目录的时候注意对应对版本) ...
- Mybatis常用总结:参数,返回,执行sql,include等
1.参数注入1.1用#{0},#{1}的形式,0代表第一个参数,1代表第二个参数 public List<RecordVo> queryList(String workerId, Inte ...
- jsp中的@include与jsp:include区别详解
1 前言 搞java开发的人也许都知道在jsp中引入项目中其他文件有如下两种方式 <%@include file="xxx.jsp"%> <jsp:include ...
- JSP中编译指令include与动作指令include的区别
include指令是编译阶段的指令,即include所包含的文件的内容是编译的时候插入到JSP文件中,JSP引擎在判断JSP页面未被修改, 否则视为已被修改.由于被包含的文件是在编译时才插入的,因此如 ...
- C/C++ 中的include
当需要使用已有的方法或库时, 可以将它们的头文件#include进来. #include会在preprocess过程中被替换成它包含的代码. 头文件中包含了需要使用的函数/变量的声明. 当然声明与定义 ...
- 织梦多语言站点,{dede:include filename=''/}引入问题
织梦模板include插入非模板目录文件出现"无法在这个位置找到"错误的解决办法 以下是dede V55_UTF8 查dede include标签手册 (3) include 引入 ...
- PHP 站点相对包含,路径的问题解决方法(include,require)
以前看了,很多框架,基本上很少使用相对路径包含.而一般很多做php web站点,喜欢用相对路径. 认为这样,无论目录放到那里. 只要跟另外目录关系一致.那么就不会出现问题.如果一个站点,一般都认为,如 ...
- 如何让include标签包裹的布局置于屏幕最下方?
如何让一个Layout 始终在屏幕的下方 我想让<include layout="@layout/bottom" />一直在屏幕下,怎么做? 1.相对布局中用属性 a ...
随机推荐
- Hibernate实现分页
转自:http://blog.csdn.net/huqiub/article/details/4329541 分页在任何系统中都是非常头疼的事情,有的数据库在语法上支持分页,而有的数据库则需要使用可滚 ...
- Memcached管理与监控工具 memAdmin
http://www.junopen.com/memadmin/ 使用MemCached以后,肯定希望知道cache的效果,对于MemCached的一些运行状态进行监控是必要的,memcached提供 ...
- linux下安装配置svn独立服务器
subversion(以下简称svn)是近年来崛起的版本管理工具,是cvs的接班人. svn服务器有2种运行方式:独立服务器和借助apache.2种方式各有利弊. file:/// 直接版本库访问(本 ...
- linux GUI程序开发
1,C++ OOP中 class与C 面向过程开发中struct非常相似
- Python学习笔记1-搭建Python环境 和 Python Hello World!
一.搭建Python开发环境 1.选择开发工具 首先要寻找一个Python的开发工具,Python的开发工具有很多,有pyCharm .Eclipse.Visual studio等等 ,使用最多的还是 ...
- MAC COCOA call command 调用终端控制台程序
MAC COCOA call command 调用终端控制台程序 STEP 1 先写一个C++ DOS程序 STEP2 使用NSTask来运行,然后用NSPipe和 NSData来接受运行的结果字符串 ...
- git 删除远程主分支及其它操作
1. 删除远程分支 如果不再需要某个远程分支了,比如搞定了某个特性并把它合并进了远程的 master 分支(或任何其他存放稳定代码的地方),可以用这个非常无厘头的语法来删除它:git push [远程 ...
- `npm install --save --save-exact react-native` failed
当你使用命令行创建一个项目 react-native init 项目名称 后出现以下错误 Installing react-native package from npm... /bin/sh: ...
- Java 工厂模式学习
工厂模式分三种:简单工厂.工厂方法.抽象工厂.其中抽象工厂是用于多个产品族的情况.所谓产品族就是不同产品组成的有机整体,各不同产品之间相互依赖.打个比方,一台电脑有CPU.主板.内存和硬盘等,这些不同 ...
- leetcode Swap Nodes in Pairs python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...