STL-算法
#include <algorithm>
1. max_element(v.begin(), v.end());
注意,所有的区间全部是半开区间,如果数组包含20~40,通过find找出25,和35的positon,但是max_element(pos25, pos35)得到的是34.
2. min_element(v.begin(), v.end());
3. find(v.begin(), v.end(), 3);
4. sort(v.begin(), v.end());
5. reverse(pos, v.end();
6. copy(v1.begin(), v1.end(), v2.begin());
7. remove(v1.begin(), v1.end(), 3);
8. for_each()
代码:
/* algorithm.cc
* 2014/09/02 update
*/
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <iterator>
using namespace std; void print(int elem) {
cout << elem << endl;
} int main() {
vector<int> v;
vector<int>::iterator pos; for(int i = ; i >= ; i--)
v.push_back(i); //max_element
pos = max_element(v.begin(), v.end());
cout << "the max element is: " << *pos << endl;
//min_element
pos = min_element(v.begin(), v.end());
cout << "the min element is: " << *pos << endl; //sort
sort(v.begin(), v.end()); //find
pos = find(v.begin(), v.end(), ); //reverse
reverse(pos, v.end()); for(pos = v.begin(); pos != v.end(); pos++)
cout << "Content of vector: " << *pos << " " << endl; //copy
vector<int> v2;
vector<int>::iterator pos1; v2.resize(v.size());
copy(v.begin(), v.end(), v2.begin());
cout << "Pre:" << endl;
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " ")); //remove
vector<int>::iterator end = remove(v2.begin(), v2.end(), );
cout << endl << "After remove(v2.begin(), v2.end(), 5) : " << endl;
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
v2.erase(end, v2.end());
cout << endl << "After erase(end, v2.end()) : " << endl;
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
//也可以使用v2.erase(remove(v2.begin(), v2.end(), 5), v2.end())) //for_each()
cout << "cout from for_each():" << endl;
for_each(v2.begin(), v2.end(), print); return ;
}
输出:
$ ./a.exe
the max element is:
the min element is:
Content of vector:
Content of vector:
Content of vector:
Content of vector:
Content of vector:
Content of vector:
Pre: After remove(v2.begin(), v2.end(), ) : After erase(end, v2.end()) : cout from for_each():
STL-算法的更多相关文章
- STL算法
STL算法部分主要由头文 件<algorithm>,<numeric>,<functional>组成.要使用 STL中的算法函数必须包含头文件<algorit ...
- 【STL源码学习】STL算法学习之四
排序算法是STL算法中相当常用的一个类别,包括部分排序和全部排序算法,依据效率和应用场景进行选择. 明细: sort 函数原型: template <class RandomAccessIter ...
- 【STL源码学习】STL算法学习之三
第一章:前言 数量不多,用到的时候会很爽. 第二章:明细 STL算法中的又一个分类:分割:将已有元素按照既定规则分割成两部分. is_partitioned 函数原型: template <c ...
- 【STL源码学习】STL算法学习之二
第一章:前言 学习笔记,记录学习STL算法的一些个人所得,在以后想用的时候可以快速拾起. 第二章:明细 copy 函数原型: template <class InputIterator, cla ...
- 【转】三十分钟学会STL算法
转载自: http://net.pku.edu.cn/~yhf/UsingSTL.htm 这是本小人书.原名是<using stl>,不知道是谁写的.不过我倒觉得很有趣,所以化了两个晚上把 ...
- random_shuffle (stl算法)打乱顺序 - 飞不会的日志 - 网易博客
random_shuffle (stl算法)打乱顺序 - 飞不会的日志 - 网易博客 random_shuffle (stl算法)打乱顺序 2012-03-31 10:39:11| 分类: 算法 | ...
- STL源代码分析——STL算法remove删除算法
前言 因为在前文的<STL算法剖析>中,源代码剖析许多.不方便学习,也不方便以后复习,这里把这些算法进行归类.对他们单独的源代码剖析进行解说.本文介绍的STL算法中的remove删除算法. ...
- STL源代码分析——STL算法merge合并算法
前言 因为在前文的<STL算法剖析>中.源代码剖析许多.不方便学习.也不方便以后复习,这里把这些算法进行归类.对他们单独的源代码剖析进行解说.本文介绍的STL算法中的merge合并算法. ...
- STL源代码分析——STL算法sort排序算法
前言 因为在前文的<STL算法剖析>中,源代码剖析许多,不方便学习,也不方便以后复习.这里把这些算法进行归类,对他们单独的源代码剖析进行解说.本文介绍的STL算法中的sort排序算法,SG ...
- 变易算法 - STL算法
欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/mutating-algorithms.h ...
随机推荐
- nyoj 28 大数阶乘
题目链接:nyoj 28 就是个简单的高精度,只是一开始我打表超内存了,然后用了各种技巧硬是把内存缩到了题目要求以下(5w+kb),感觉挺爽的,代码如下: #include<cstdio> ...
- linux 文件类型 文件权限
linux中常见的文件类型有: “—”表示普通文件 :-rw-r--r-- 1 root root 41727 07-13 02:56 install.log “d”表示目录 :drwxr-xr- ...
- Yii2.0 依赖注入(DI)和依赖注入容器的原理
依赖注入和依赖注入容器 为了降低代码耦合程度,提高项目的可维护性,Yii采用多许多当下最流行又相对成熟的设计模式,包括了依赖注入(Denpdency Injection, DI)和服务定位器(Serv ...
- Java用WebSocket + tail命令实现Web实时日志
原文:http://blog.csdn.net/xiao__gui/article/details/50041673 在Linux操作系统中,经常需要查看日志文件的实时输出内容,通常会使用tail - ...
- windows服务名称不是单个单词的如何启动?
比如要启动 Memcached Server: 命令行中输入命令要注意大小写,大小写要保持一致,正确的写法如下: net start "Memcached Server". ...
- jQuery中其他
hide: 隐藏 $('img').hide(); show:显示 $('img').show(); 单选多选下拉菜单 选中状态checked ($('.radio:checked')); 单选 ( ...
- Deep Learning in Bioinformatics
最近在学tensorflow,深度学习的开源工具,很好奇在生信领域深度学习都能做些什么东西. 镇楼的综述:Deep Learning in Bioinformatics 几篇文章读读看: Deep l ...
- 用NAN简化Google V8 JS引擎的扩展
通过C++扩展Google V8 JS引擎的文章很多,Google V8 JS带的例子也容易明白.但是大部分文章都是Hello World型的,真正使用时发现处处是坑.扩展V8最经典的例子就是node ...
- ajax获取数据库中数据
xhr=new XMLHttpRequest(); var url="要获取数据的地方"; xhr.open('post',url,true); POST请求头(get就不用写这个 ...
- js正则--验证6-12位至少包含数字、小写字母和大些字母中至少两种字符,
var reg=/^((([a-z])+([0-9])+)|(([0-9])+([a-z])+)|(([A-Z])+([0-9])+)|(([0-9])+([A-Z])+)|(([a-z])+([A- ...