#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-算法的更多相关文章

  1. STL算法

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

  2. 【STL源码学习】STL算法学习之四

    排序算法是STL算法中相当常用的一个类别,包括部分排序和全部排序算法,依据效率和应用场景进行选择. 明细: sort 函数原型: template <class RandomAccessIter ...

  3. 【STL源码学习】STL算法学习之三

    第一章:前言 数量不多,用到的时候会很爽. 第二章:明细 STL算法中的又一个分类:分割:将已有元素按照既定规则分割成两部分.  is_partitioned 函数原型: template <c ...

  4. 【STL源码学习】STL算法学习之二

    第一章:前言 学习笔记,记录学习STL算法的一些个人所得,在以后想用的时候可以快速拾起. 第二章:明细 copy 函数原型: template <class InputIterator, cla ...

  5. 【转】三十分钟学会STL算法

    转载自: http://net.pku.edu.cn/~yhf/UsingSTL.htm 这是本小人书.原名是<using stl>,不知道是谁写的.不过我倒觉得很有趣,所以化了两个晚上把 ...

  6. random_shuffle (stl算法)打乱顺序 - 飞不会的日志 - 网易博客

    random_shuffle (stl算法)打乱顺序 - 飞不会的日志 - 网易博客 random_shuffle (stl算法)打乱顺序 2012-03-31 10:39:11|  分类: 算法 | ...

  7. STL源代码分析——STL算法remove删除算法

    前言 因为在前文的<STL算法剖析>中,源代码剖析许多.不方便学习,也不方便以后复习,这里把这些算法进行归类.对他们单独的源代码剖析进行解说.本文介绍的STL算法中的remove删除算法. ...

  8. STL源代码分析——STL算法merge合并算法

    前言 因为在前文的<STL算法剖析>中.源代码剖析许多.不方便学习.也不方便以后复习,这里把这些算法进行归类.对他们单独的源代码剖析进行解说.本文介绍的STL算法中的merge合并算法. ...

  9. STL源代码分析——STL算法sort排序算法

    前言 因为在前文的<STL算法剖析>中,源代码剖析许多,不方便学习,也不方便以后复习.这里把这些算法进行归类,对他们单独的源代码剖析进行解说.本文介绍的STL算法中的sort排序算法,SG ...

  10. 变易算法 - STL算法

    欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/mutating-algorithms.h ...

随机推荐

  1. shell脚本中获取本机ip地址的方法

    ipaddr='172.0.0.1' ipaddr=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/ ...

  2. easyui combobox 智能提示搜索

    <!-- 获取机会点名称列表 --><script> function initOpportunityNameFuzzyQuery() { $('#jihuidianmingc ...

  3. entity refenrece 在views中的运用

    在一个content type中有一个field是entity reference, 那么这个字段的设置过程中会指定一个entity type和content type和一个具体内容的选择器, 然后到 ...

  4. 堆排序(C语言)

    #ifndef HEAP_SORT_H #define HEAP_SROT_H #include<iostream> void maxHeap(int *arr,unsigned int ...

  5. html5中的表单

    <form id="aForm" action="reg.php"> <p>请填写表单内容以完成注册!</p> <fi ...

  6. IE7浏览器下CSS属性选择器二三事

    一.为何专门说起IE7 以前,或者说数年前,我们从事桌面端网页开发的时候,基本上都还要兼顾IE6浏览器, 即使有些特性,IE7支持,我们也会忽略之.于是,我们会不自然地把IE6和IE7浏览器归为一路货 ...

  7. selenium+python笔记7

    #!/usr/bin/env python # -*- coding: utf-8 -*- """ @desc: 测试126邮箱的登陆功能 1.使用公共方法public. ...

  8. Unique Paths [LeetCode]

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  9. SQL Server数据库(高级查询)

    高级查询 1.连接查询 有外键关系的两张表,通过关系一次查询两张表 (1)select * from 表名,表名 --- 直接使用出现笛卡尔积,两个表数据一一对应,查询出的结果数目是两个表的行的乘积, ...

  10. javascript 数组操作 转

    javascript之数组操作 1.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一 ...