浅析STL中的常用算法

 更新时间:2013年09月25日 09:57:59   作者:  
以下是对STL中的常用算法进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助

一、非变异算法

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

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;

}


5 n次填充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;

}

相关文章

  • C++ 手撸简易服务器

    C++ 手撸简易服务器

    本文主要介绍了C++ 手撸简易服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • C语言数据结构中约瑟夫环问题探究

    C语言数据结构中约瑟夫环问题探究

    这篇文章主要介绍了C语言数据结构中约瑟夫环问题,总的来说这并不是一道难题,那为什么要拿出这道题介绍?拿出这道题真正想要传达的是解题的思路,以及不断优化探寻最优解的过程。希望通过这道题能给你带来一种解题优化的思路
    2023-01-01
  • 带头结点单链表(详解)

    带头结点单链表(详解)

    这篇文章主要介绍了带头结点单链表 (详解),需要的朋友可以参考下
    2023-07-07
  • C++中拷贝构造函数的总结详解

    C++中拷贝构造函数的总结详解

    深拷贝和浅拷贝可以简单理解为:如果一个类拥有资源,当这个类的对象发生复制过程的时候,资源重新分配,这个过程就是深拷贝,反之,没有重新分配资源,就是浅拷贝
    2013-09-09
  • 快来领取!你想要的C++/C语言优秀书籍

    快来领取!你想要的C++/C语言优秀书籍

    如何选择合适的C++/C语言书籍,是不是已经眼花缭乱,不知道该选择哪本好了呢?今天我来为大家分享两本不可错过的优秀书籍
    2017-09-09
  • 深入分析Visual C++进行串口通信编程的详解

    深入分析Visual C++进行串口通信编程的详解

    本篇文章是对Visual C++进行串口通信编程进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • C++与QML进行数据交互实现方式介绍

    C++与QML进行数据交互实现方式介绍

    迫于无奈开始写android的程序,以前使用QWidget的方式试过,虽然界面可以实现,但是最后调用摄像头时,未能成功,再没有继续。这几天开始使用qml进行尝试,在使用的过程中,其中的一个难点,就是在qml与c++中数据的交互
    2022-09-09
  • C++实现 vector 的四则运算

    C++实现 vector 的四则运算

    本文给大家介绍的是在C++中实现高效的vector四则运算的方法的相关资料,需要的朋友可以参考下
    2016-07-07
  • C/C++举例讲解关键字的用法

    C/C++举例讲解关键字的用法

    相对于其他语言来说,C语言的关键字算是少的了。在C98中关键子总共只有32个,我们来分析一下部分关键字在C/C++中它独特的作用
    2022-05-05
  • C++ HLSL实现简单的图像处理功能

    C++ HLSL实现简单的图像处理功能

    本文主要介绍了HLSL实现简单的图像处理功能的方法,具有很好的参考价值,下面跟着小编一起来看下吧
    2017-02-02

最新评论