zl程序教程

您现在的位置是:首页 >  后端

当前栏目

c++11新增的便利算法实例分析

C++实例算法 分析 11 新增 便利
2023-06-13 09:15:45 时间

C++是一门应用非常广泛的程序设计语言,而c++11则新增加了一些便利的算法,这些新增的算法使我们的代码写起来更简洁方便,本文列举一些常用的新增算法,算是做个总结分析,更多的新增算法读者可以参考:http://en.cppreference.com/w/cpp/algorithm。

算法库新增了三个用于判断的算法all_of、any_of和none_of,定义如下:

template<classInputIt,classUnaryPredicate>
boolall_of(InputItfirst,InputItlast,UnaryPredicatep);

template<classInputIt,classUnaryPredicate>
boolany_of(InputItfirst,InputItlast,UnaryPredicatep);

template<classInputIt,classUnaryPredicate>
boolnone_of(InputItfirst,InputItlast,UnaryPredicatep);

①all_of:检查区间[first,last)中是否所有的元素都满足一元判断式p,所有的元素都满足条件返回true,否则返回false。
②any_of:检查区间[first,last)中是否至少有一个元素都满足一元判断式p,只要有一个元素满足条件就返回true,否则返回true。
③none_of:检查区间[first,last)中是否所有的元素都不满足一元判断式p,所有的元素都不满足条件返回true,否则返回false。

下面是这几个算法的示例:

#include<iostream>
#include<algorithm>
#include<vector>
usingnamespacestd;
intmain()
{
vector<int>v={1,3,5,7,9};
  autoisEven=[](inti){returni%2!=0;
boolisallOdd=std::all_of(v.begin(),v.end(),isEven);
if(isallOdd)
cout<<"allisodd"<<endl;

boolisNoneEven=std::none_of(v.begin(),v.end(),isEven);
if(isNoneEven)
cout<<"noneiseven"<<endl;

vector<int>v1={1,3,5,7,8,9};
boolanyof=std::any_of(v1.begin(),v1.end(),isEven);
if(anyof)
cout<<"atleastoneiseven"<<endl;
}

输出:

allisodd
noneisodd
atleastoneiseven

算法库的查找算法新增了一个find_if_not,它的含义和find_if是相反的,即查找不符合某个条件的元素,find_if也可以实现find_if_not的功能,只需要将判断式改为否定的判断式即可,现在新增了find_if_not之后,就不需要再写否定的判断式了,可读性也变得更好。下面是它的基本用法:

#include<iostream>
#include<algorithm>
#include<vector>
usingnamespacestd;

intmain()
{
vector<int>v={1,3,5,7,9,4};
autoisEven=[](inti){returni%2==0;};
autofirstEven=std::find_if(v.begin(),v.end(),isEven);
if(firstEven!=v.end())
cout<<"thefirstevenis"<<*firstEven<<endl;

//用find_if来查找奇数则需要重新写一个否定含义的判断式
  autoisNotEven=[](inti){returni%2!=0;};
  autofirstOdd=std::find_if(v.begin(),v.end(),isNotEven);

if(firstOdd!=v.end())
cout<<"thefirstoddis"<<*firstOdd<<endl;

//用find_if_not来查找奇数则无需新定义判断式

autoodd=std::find_if_not(v.begin(),v.end(),isEven);
if(odd!=v.end())
cout<<"thefirstoddis"<<*odd<<endl;
}

将输出:

thefirstevenis4
thefirstoddis1
thefirstoddis1

可以看到使用find_if_not不需要再定义新的否定含义的判断式了,更简便了。

算法库还增加了一个copy_if算法,它相比原来的copy算法多了一个判断式,用起来更方便了,下面是它的基本用法:

#include<iostream>
#include<algorithm>
#include<vector>
usingnamespacestd;

intmain()
{
vector<int>v={1,3,5,7,9,4};
std::vector<int>v1(v.size());
    //根据条件拷贝
    autoit=std::copy_if(v.begin(),v.end(),v1.begin(),[](inti){returni%2!=0;});
    //缩减vector到合适大小
  v1.resize(std::distance(v1.begin(),it));
for(inti:v1)
{
cout<<i<<"";
}

cout<<endl;
}

算法库新增了iota用来方便的生成有序序列,比如我们需要一个定长数组,这个数组中的元素都是在某一个数值的基础之上递增的,那么用iota可以很方便的生成这个数组了。下面是它的基本用法:

#include<numeric>
#include<array>
#include<vector>
#include<iostream>
usingnamespacestd;

intmain()
{
vector<int>v(4);
//循环遍历赋值来初始化数组
//for(inti=1;i<=4;i++)
//{
//v.push_back(i);
//}

//直接通过iota初始化数组,更简洁
std::iota(v.begin(),v.end(),1);
for(auton:v){
cout<<n<<"";
}
cout<<endl;

std::array<int,4>array;
std::iota(array.begin(),array.end(),1);
for(auton:array){
cout<<n<<"";
}
std::cout<<endl;
}

将输出:

1234
1234

可以看到使用iota比遍历赋值来初始化数组更简洁,需要注意的是iota初始化的序列需要指定大小,如果上面的代码中:vector<int>v(4);没有指定初始化大小为4的话,则输出为空。

算法库还新增了一个同时获取最大值和最小值的算法minmax_element,这样我们如果想获取最大值和最小值的时候就不用分别调用max_element和max_element算法了,用起来会更方便,minmax_element会将最小值和最大值的迭代器放到一个pair中返回,下面是它的基本用法:

#include<iostream>
#include<algorithm>
#include<vector>
usingnamespacestd;

intmain(){
//yourcodegoeshere
vector<int>v={1,2,5,7,9,4};
autoresult=minmax_element(v.begin(),v.end());

cout<<*result.first<<""<<*result.second<<endl;

return0;
}

将输出:

19

算法库新增了is_sorted和is_sorted_until算法,is_sort用来判断某个序列是否是排好序的,is_sort_until则用来返回序列中前面已经排好序的部分序列。下面是它们的基本用法:

#include<iostream>
#include<algorithm>
#include<vector>
usingnamespacestd;

intmain(){
vector<int>v={1,2,5,7,9,4};
autopos=is_sorted_until(v.begin(),v.end());

for(autoit=v.begin();it!=pos;++it)
{
cout<<*it<<"";
}
cout<<endl;

boolis_sort=is_sorted(v.begin(),v.end());
cout<<is_sort<<endl;
return0;
}

将输出:

12579
0

总结:这些新增的算法让我们用起来更加简便,也增强了代码的可读性。

希望本文所述算法对大家更好的掌握C++11能有所帮助。