[C++] algorithm库内容学习(1)

本文详细介绍了C++ algorithm库中的几个重要函数,包括std::all_of、std::any_of、std::none_of,以及std::for_each、std::find、std::find_if等,涵盖了寻找、判断和遍历等功能,提供了相应的用例说明。
摘要由CSDN通过智能技术生成

algorithm

[C++ 11] std::all_of

原型:

template <class InputIterator, class UnaryPredicate>
  bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred);

功能: 指定上下界内是否都符合某条件
用例:

#include<iostream>
#include<algorithm>
using namespace std;

int main(int argc, char *argv[])
{
    int numbers[] = { 1, 3, 5, 7, 9, 11 };

    if (all_of(begin(numbers), end(numbers), [](int i)
    {
        return i & 1;
    })) {
        cout << "所有的数都是奇数" << endl;
    } else {
        cout << "部分数字不是奇数" << endl;
    }

    return 0;
}

[C++ 11] std::any_of

原型:

template <class InputIterator, class UnaryPredicate>
  bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred);

功能: 测试指定上下界内是否有元素满足条件
用例:

#include<iostream>
#include<algorithm>

using namespace std;

int main(int argc, char *argv[])
{
    int numbers[] = { 1, 3, 5, 7, 9, 11, 6 };

    if (any_of(begin(numbers), end(numbers), [](int i)
    {
        return i & 1;
    })) {
        cout << "数组中存在偶数" << endl;
    } else {
        cout << "数组中不存在偶数" << endl;
    }

    return 0;
}

[C++ 11] std::none_of

原型:

template <class InputIterator, class UnaryPredicate>
  bool none_of (InputIterator first, InputIterator last, UnaryPredicate pred);

功能: 测试指定上下界内是否均不符合某一条件
用例:

#include<iostream>
#include<algorithm>

using namespace std;

int main(int argc, char *argv[])
{
    int numbers[] = { 1, 3, 5, 7, 9, 11, 6 };

    if (none_of(begin(numbers), end(numbers), [](int i)
    {
        return i & 1;
    })) {
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值