C++11中算法库新增的一些算法函数

1.1 all_ofany_ofnone_of

template< class InputIt, class UnaryPredicate >

bool all_of( InputIt first, InputIt last, UnaryPredicate p );



template< class InputIt, class UnaryPredicate >

bool any_of( InputIt first, InputIt last, UnaryPredicate p );



template< class InputIt, class UnaryPredicate >

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

1.2 find_if 算法

实例:

#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;



int main()

{

    vector<int> v = { 1, 3, 5, 7, 9,4 };

    auto isEven = [](int i){return i % 2 == 0;};

    auto firstEven = std::find_if(v.begin(), v.end(), isEven);

    if (firstEven!=v.end())

              cout << "the first even is " <<* firstEven << endl;



       //用find_if来查找奇数则需要重新写一个否定含义的判断式

  auto isNotEven = [](int i){return i % 2 != 0;};

  auto firstOdd = std::find_if(v.begin(), v.end(),isNotEven);



       if (firstOdd!=v.end())

              cout << "the first odd is " <<* firstOdd << endl;



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



       auto odd = std::find_if_not(v.begin(), v.end(), isEven);

       if (odd!=v.end())

              cout << "the first odd is " <<* odd << endl;

}

1.3 copy_if算法

用法:

#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;



int main()

{

       vector<int> v = { 1, 3, 5, 7, 9, 4 };

       std::vector<int> v1(v.size());

    //根据条件拷贝

    auto it = std::copy_if(v.begin(), v.end(), v1.begin(), [](int i){return i%2!=0;});

    //缩减vector到合适大小

      v1.resize(std::distance(v1.begin(),it));

       for(int i : v1)

       {

              cout<<i<<" ";

       }



       cout<<endl;

}   

1.4 iota算法

#include <numeric>

#include <array>

#include <vector>

#include <iostream>

using namespace std;



int main()

{

vector<int> v(4) ;



std::iota(v.begin(), v.end(), 1);

    for(auto n: v) {

        cout << n << ' ';

    }

    cout << endl;

   

    std::array<int, 4> array;

    std::iota(array.begin(), array.end(), 1);

    for(auto n: array) {

        cout << n << ' ';

    }

    std::cout << endl;

}

1.5 minmax_element算法

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

#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;



int main() {

    // your code goes here

    vector<int> v = { 1, 2, 5, 7, 9, 4 };

    auto result = minmax_element(v.begin(), v.end());

  

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



    return 0;

}

1.6 is_ sortedis_ sorted_until算法

#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;



int main() {

    vector<int> v = { 1, 2, 5, 7, 9, 4 };

    auto pos = is_sorted_until(v.begin(), v.end());

  

    for(auto it=v.begin(); it!=pos; ++it)

    {

        cout<<*it<< " ";

    }

    cout<<endl;

   

    bool is_sort = is_sorted(v.begin(), v.end());

    cout<< is_sort<<endl;

    return 0;

}

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值