一、非变异算法

是一组不破坏操作数据的模板函数,用来对序列数据进行逐个处理、元素查找、子序列搜索、统计和匹配。非变异算法具有极为广泛的适用性,基本上可应用与各种容器。

1查找容器元素find

它用于查找等于某值的元素。它在迭代器区间[first,last)(闭开区间)上查找等于value值的元素,假设迭代器i所指的元素满足*i=value,则返回迭代器i;未找到满足条件的元素,返回last。函数原型:find( v1.begin(), v1.end(), num_to_find );

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

void main()

{

int num_to_find = 6;

vector<int> v1;

for( int i = 0; i < 10; i++ )

v1.push_back(2*i);

vector<int>::iterator result;

result = find( v1.begin(), v1.end(), num_to_find );

if( result == v1.end() )

cout << "未找到不论什么元素匹配 " << num_to_find << endl;

else

cout << "匹配元素的索引值是 " << result-v1.begin() << endl;

}

2条件查找容器元素find_if

利用返回布尔值的谓词推断pred,检查迭代器区间[first,last)(闭开区间)上的每个元素,假设迭代器i满足pred(*i)=true,表示找到元素并返回迭代值i(找到的第一个符合条件的元素);未找到元素,返回末位置last。函数原型:find_if(v.begin(),v.end(),divby5);

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

bool divby5(int x)

{

return x%5?0:1;

}

void main()

{

vector<int> v(20);

for(int i=0;i<v.size();i++)

{

v[i]=(i+1)*(i+3);

cout<<v[i]<<' ';

}

cout<<endl;

vector<int>::iterator ilocation;

ilocation=find_if(v.begin(),v.end(),divby5);

if(ilocation!=v.end())

cout<<"找到第一个能被5整除的元素:"<<*ilocation<<endl<<"元素的索引位置是: "<<ilocation-v.begin()<<endl;

}

3统计等于某值的容器元素个数count

list<int> l;

count(l.begin(),l.end(),value)

4条件统计count_if

count_if(l.begin(),l.end(),pred)。谓词pred含义同find_if中的谓词。样例能够參考例2.

5子序列搜索search

search算法函数在一个序列中搜索与还有一序列匹配的子序列。參数分别为一个序列的開始位置,结束位置和还有一个序列的開始,结束位置。

函数原型:search(v1.begin(),v1.end(),v2.begin(),v2.end());

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

void main()

{

vector<int> v1;

cout<<"v1:";

for(int i=0;i<5;i++)

{

v1.push_back(i+5);

//注意:v1定义时没有给定大小,因此这里不能直接使用赋值语句。

cout<<v1[i]<<' ';

}

cout<<endl;

vector<int> v2;

cout<<"v2:";

for(i=0;i<2;i++)

{

v2.push_back(i+7);

cout<<v2[i]<<' ';

}

cout<<endl;

vector<int>::iterator ilocation;

ilocation=search(v1.begin(),v1.end(),v2.begin(),v2.end());

if(ilocation!=v1.end())

cout<<"v2的元素包括在v1中,起始元素为"<<"v1["<<ilocation-v1.begin()<<']'<<endl;

else

cout<<"v2的元素不包括在v1中"<<endl;

}

6反复元素子序列搜索search_n

search_n算法函数搜索序列中是否有一系列元素值均为某个给定值的子序列。函数原型:search_n(v.begin(),v.end(),3,8),在v中找到3个连续的元素8

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

void main()

{

vector<int> v;

v.push_back(1);

v.push_back(8);

v.push_back(8);

v.push_back(8);

v.push_back(6);

v.push_back(6);

v.push_back(8);

vector<int>::iterator i;

i=search_n(v.begin(),v.end(),3,8);

if(i!=v.end())

cout<<"在v中找到3个连续的元素8"<<endl;

else

cout<<"在v中未找到3个连续的元素8"<<endl;

}

7最后一个子序列搜索find_end

函数原型find_end(v1.begin(),v1.end(),v2.begin(),v2.end());在V1中要求的位置查找V2中要求的序列。

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

void main()

{

vector<int> v1;

v1.push_back(-5);

v1.push_back(1);

v1.push_back(2);

v1.push_back(-6);

v1.push_back(-8);

v1.push_back(1);

v1.push_back(2);

v1.push_back(-11);

vector<int> v2;

v2.push_back(1);

v2.push_back(2);

vector<int>::iterator i;

i=find_end(v1.begin(),v1.end(),v2.begin(),v2.end());

if(i!=v1.end())

cout<<"v1中找到最后一个匹配v2的子序列,位置在" <<"v1["<<i-v1.begin()<<"]"<<endl;

}

二、变异算法

是一组可以改动容器元素数据的模板函数。copy(v.begin(),v.end(),l.begin());将v中的元素拷贝到l中。

1元素复制copy

#include <vector>

#include <list>

#include <algorithm>

#include <iostream>

using namespace std;

void main()

{

vector<int> v;

v.push_back(1);

v.push_back(3);

v.push_back(5);

list<int> l;

l.push_back(2);

l.push_back(4);

l.push_back(6);

l.push_back(8);

l.push_back(10);

copy(v.begin(),v.end(),l.begin());

list<int>::iterator i;

for(i=l.begin();i!=l.end();i++)

cout<<*i<<' ';

cout<<endl;

}

2元素变换transform改变

函数原型:transform(v.begin(),v.end(),l.begin(),square);也是复制,可是要按某种方案复制。

#include <vector>

#include <list>

#include <algorithm>

#include <iostream>

using namespace std;

int square(int x)

{

return x*x;

}

void main()

{

vector<int> v;

v.push_back(5);

v.push_back(15);

v.push_back(25);

list<int> l(3);

transform(v.begin(),v.end(),l.begin(),square);

list<int>::iterator i;

for(i=l.begin();i!=l.end();i++)

cout<<*i<<' ';

cout<<endl;

}

3替换replace

replace算法将指定元素值替换为新值。

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

void main()

{

vector<int> v;

v.push_back(13);

v.push_back(25);

v.push_back(27);

v.push_back(25);

v.push_back(29);

replace(v.begin(),v.end(),25,100);

vector<int>::iterator i;

for(i=v.begin();i!=v.end();i++)

cout<<*i<<' ';

cout<<endl;

}

输出结果为13 100 27 100 29

4条件替换replace_if

函数原型:replace_if(v.begin(),v.end(),odd,100);

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

bool odd(int x)

{

return x%2;

}

void main()

{

vector<int> v;

for(int i=1;i<10;i++)

v.push_back(i);

replace_if(v.begin(),v.end(),odd,100);

vector<int>::iterator ilocation;

for(ilocation=v.begin();ilocation!=v.end();ilocation++)

cout<<*ilocation<<' ';

cout<<endl;

}

5n次填充fill_n

函数原型fill_n(v.begin(),5,-1);向从v.begin開始的后面5个位置跳入-1

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

void main()

{

vector<int> v(10);

fill_n(v.begin(),5,-1);

vector<int>::iterator ilocation;

for(ilocation=v.begin();ilocation!=v.end();ilocation++)

cout<<*ilocation<<' ';

cout<<endl;

}

输出结果:-1 -1 -1 -1 -1 0 0 0 0 0

6随机生成n个元素generate

函数原型:generate_n(v.begin(),5,rand);向从v.begin開始的后面5个位置随机填写数据。

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

void main()

{

vector<int> v(10);

generate_n(v.begin(),5,rand);

vector<int>::iterator ilocation;

for(ilocation=v.begin();ilocation!=v.end();ilocation++)

cout<<*ilocation<<' ';

cout<<endl;

}

7条件移除remove_if

返回值相当于移除满足条件的元素后形成的新向量的end()值。

函数原型:remove_if(v.begin(),v.end(),even);

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

bool even(int x)

{

return x%2?0:1;

}

void main()

{

vector<int> v;

for(int i=1;i<=10;i++)

v.push_back(i);

vector<int>::iterator ilocation,result;

cout<<"移除前:";

for(ilocation=v.begin();ilocation!=v.end();ilocation++)

cout<<*ilocation<<' ';

cout<<endl;

result=remove_if(v.begin(),v.end(),even);

cout<<"移除后:";

for(ilocation=v.begin();ilocation!=result;ilocation++)

cout<<*ilocation<<' ';

cout<<endl;

}

8剔除连续反复元素unique

函数原型:unique(v.begin(),v.end());

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

void main()

{

vector<int> v;

v.push_back(2);

v.push_back(6);

v.push_back(6);

v.push_back(6);

v.push_back(9);

v.push_back(6);

v.push_back(3);

vector<int>::iterator ilocation,result;

result=unique(v.begin(),v.end());

for(ilocation=v.begin();ilocation!=result;ilocation++)

cout<<*ilocation<<' ';

cout<<endl;

}

输出结果:2 6 9 6 3

三、排序算法

1、创建堆make_heap

2、元素入堆push_heap(默认插入最后一个元素)

3、元素出堆pop_heap(与push_heap一样,pop_heap必须对堆操作才有意义)

#include <vector>??????

#include <algorithm>

#include <iostream>

using namespace std;

void main()

{

vector<int> v;

v.push_back(5);

v.push_back(6);

v.push_back(4);

v.push_back(8);

v.push_back(2);

v.push_back(3);

v.push_back(7);

v.push_back(1);

v.push_back(9);

make_heap(v.begin(),v.end());

v.push_back(20);

push_heap(v.begin(),v.end());

vector<int>::iterator ilocation;

for(ilocation=v.begin();ilocation!=v.end();ilocation++)

cout<<*ilocation<<' ';

cout<<endl;

pop_heap(v.begin(),v.end());

for(ilocation=v.begin();ilocation!=v.end();ilocation++)

cout<<*ilocation<<' ';

cout<<endl;

}

4堆排序sort_heap

使用:

make_heap(v.begin(),v.end());

sort_heap(v.begin(),v.end());

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

void main()

{

vector<int> v;

v.push_back(3);

v.push_back(9);

v.push_back(6);

v.push_back(3);

v.push_back(17);

v.push_back(20);

v.push_back(12);

vector<int>::iterator ilocation;

for(ilocation=v.begin();ilocation!=v.end();ilocation++)

cout<<*ilocation<<' ';

cout<<endl;

make_heap(v.begin(),v.end());

sort_heap(v.begin(),v.end());

for(ilocation=v.begin();ilocation!=v.end();ilocation++)

cout<<*ilocation<<' ';

cout<<endl;

}

输出结果:

3 9 6 3 17 20 12

3 3 6 9 12 17 20

5排序sort

函数原型:sort(v.begin(),v.end());

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

void main()

{

vector<int> v;

v.push_back(2);

v.push_back(8);

v.push_back(-15);

v.push_back(90);

v.push_back(26);

v.push_back(7);

v.push_back(23);

v.push_back(30);

v.push_back(-27);

v.push_back(39);

v.push_back(55);

vector<int>::iterator ilocation;

for(ilocation=v.begin();ilocation!=v.end();ilocation++)

cout<<*ilocation<<' ';

cout<<endl;

sort(v.begin(),v.end());//比較函数默认

for(ilocation=v.begin();ilocation!=v.end();ilocation++)

cout<<*ilocation<<' ';

cout<<endl;

}

STL之涉及到的算法相关推荐

  1. STL (13) 非变动型算法

    STL (13) 非变动型算法 algorithm是"算法"必须的头文件. Non-modifying sequence operations (非变动式算法):算法过后,容器内部 ...

  2. STL常用的算术和生成算法

    STL常用的算术和生成算法 accumulate() fill() accumulate() accumulate: 对指定范围内的元素求和,然后结果再加上一个由val指定的初始值. #include ...

  3. 基于STL实现自动贪心寻路算法的贪吃蛇小游戏

    基于STL实现自动贪心寻路算法的贪吃蛇小游戏 写贪吃蛇小游戏的想法来自CometOJ-Contest#13的B题,当时用STL双端队列维护蛇身的时候觉得非常方便,现在用EasyX图形库实现一下. 运行 ...

  4. C语言幸运星课程设计,涉及约瑟夫环算法,多个版本

    C语言幸运星课程设计,涉及约瑟夫环算法,多个版本,放于多个分支. 使用及转载请标明出处(最好点个赞及star哈哈) 版本1使用链表,函数传参 版本2使用链表,空参函数,全局变量 版本3使用动态数组 码 ...

  5. C++练习笔记STL模板库之常用算法1

    STL算法组成 STL算法主要是由头文件 组成 是所有STL头文件中最大的一个,范围涉及到比较.交换.查找.遍历.复制.修改等 体积很小,只包括几个在序列上面进行简单数学运算的模板函数 定义了一些模板 ...

  6. C++拾取——使用stl标准库实现排序算法及评测

    今天看了一篇文章,讲各种语言的优势和劣势.其中一个观点:haskell非常适合写算法,因为使用者不用去关心具体的计算机实现,而只要关注于操作语义.这让它在专心研究算法的人中非常受欢迎.所以很多时候,语 ...

  7. stl clocklist 查找元素_C++算法竞赛中常用的STL

    什么是STL? STL,Standard Template Library的缩写,标准模版库的意思.STL是一些"容器"的集合,这些容器包括list. vector.set.que ...

  8. STL源码剖析 基本算法 < stl_algobase.h >

    注意事项 : 实际使用的时候,使用的是<algorithm>这个头文件,不是题目中的< stl_algobase.h > equal函数 如果两个序列在[firsLlast) ...

  9. 【STL学习】堆相关算法详解与C++编程实现(Heap)

    堆简介 堆并不是STL的组件,但是经常充当着底层实现结构.比如优先级队列(Priority Queue)等等. 堆是一种完全二叉树,因此我们可以用数组来存储所有节点.在这里的实现中,采用了一个技巧:将 ...

最新文章

  1. kibana智能检索发送多次_msearch —— 配置index pattern,同时设置时间段,就知道到底是在哪些索引里去查找数据了...
  2. 手工纸盒子_不锈钢水槽如何选购,拉伸水槽与手工槽制造工艺有何区别
  3. 项目中遇到不善于表达的人,该如何沟通?
  4. 《数据库SQL实战》从titles表获取按照title进行分组
  5. SAP CRM呼叫中心点击了End按钮后,会进行呼叫记录关系的保存
  6. 自动取款机如何使用无卡取款_云南铝管自动抛光机如何使用_利琦抛光机械
  7. 【DevOps进行时】自动化测试之单元测试
  8. word把选择答案弄到题目里_word中把选择题的正确答案自动填到括号里技巧
  9. 没想到,我都来阿里5年了!
  10. 说一下你对多态的理解?_如何去理解java中的多态?从jvm角度分析也许让你更清晰...
  11. centos配置maven环境
  12. 查看Android打包时签名文件keystore的MD5值
  13. 微软笔试题-c语言-算法分析
  14. 计算机本地磁盘设密码,电脑硬盘,教您电脑硬盘怎么设置密码
  15. 数字功放和模拟功放差异介绍
  16. 无理数平方根计算_如何找到数字的平方根并手动计算
  17. 深度学习——UMRL
  18. 百度人脸产品套件————壁虎 1.开箱体验
  19. 三菱FX系列PLC模拟量输入AD模块的使用方法和相关编程设置详解
  20. 使用Spark和Pig统计每秒钟微博数量

热门文章

  1. json字符串多了双引号_Python-数据解析-json模块 !
  2. r语言岭回归参数选择_数据分析中常见的七种回归分析以及R语言实现(三)---岭回归...
  3. linux 删除N天文件
  4. 什么是jQuery?
  5. 查询商品列表报错This application has no explicit mapping for /error, so you are seeing this as a fallback
  6. 安卓学习笔记03:安卓应用目录结构
  7. Java实训项目:GUI学生信息管理系统(2017)
  8. 【HDU5156】Harry and Christmas tree,两种离线的做法
  9. 3.过滤——相关滤波(Correlation Filtering)_3
  10. 计算机应用从组织内部,全国2014年10月自考管理系统中计算机应用试题和答案