C++学习_算法_九(一)

本文深入探讨了C++标准库中的算法部分,包括用于测试序列条件的all_of()、any_of()、none_of()函数,以及序列遍历、查找、计数等功能。详细介绍了find()、find_if()、count()等函数的应用场景和使用方法。

介绍

algorithm算法库拥有非常多算法,这可以节省不少力气,这些库主要是针对容器库的

不修改序列的操作

all_of(),any_of(),none_of()

这些函数测试序列是否满足某一条件

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    vector<int> a = {2, 4, 6, 8, 10};
    bool b = all_of(a.begin(), a.end(), [](int i) {
        return i % 2 == 0;
    });
    bool c = any_of(a.begin(), a.end(), [](int i) {
        return i % 2 == 0;
    });
    bool d = none_of(a.begin(), a.end(), [](int i) {
        return i % 2 == 0;
    });
    cout << b << "\n";
    cout << c << "\n";
    cout << d;

    return 0;
}

返回结果是

1
1
0

for_each()

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    vector<int> a = {2, 4, 6, 8, 10};
    for_each(a.begin(), a.end(), [](int i) {
        cout << i << " ";
    });

    return 0;
}

查找函数

find()

查找第一个关键字

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    vector<int> a = {2, 4, 6, 8, 10};
    int b[2] = {8, 10};
    auto it = find(a.begin(), a.end(), 10);
    cout << *it;
    return 0;
}

find_if(),find_if_not()

查找第一个满足条件的元素

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    vector<int> a = {2, 4, 6, 8, 10};
    int b[2] = {8, 10};
    auto it = find_if(a.begin(), a.end(), [](int i) {
        return i == 10;
    });
    cout << *it;
    return 0;
}

find_end()

查找最后满足的子序列,有子序列则返回子序列第一个元素的迭代器,否则返回0

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    vector<int> a = {2, 4, 6, 8, 10};
    int b[2] = {8, 10};
    auto it = find_end(a.begin(), a.end(), b, b + 2);
    cout << *it;
    return 0;
}

find_first_of()

find_first_of:查找A序列中第一个与序列B中任一元素值相等的元素位置

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    vector<int> a = {2, 4, 6, 8, 10};
    int b[2] = {8, 10};
    auto it = find_first_of(a.begin(), a.end(), b, b + 2);
    cout << *it;
    return 0;
}

adjacent_find()

查找相等的相邻元素

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    vector<int> a = {2, 4, 6, 8, 8};
    int b[2] = {8, 10};
    auto it = adjacent_find(a.begin(), a.end());
    cout << *it;
    return 0;
}

有则返回第一对相同元素的迭代器,无相邻相同元素则返回0

count()

计算某一个值的元素数

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    vector<int> a = {2, 4, 6, 8, 8, 9, 9};
    int b[2] = {8, 10};
    int c = count(a.begin(), a.end(), 8);
    cout << c;
    return 0;
}

count_if()

查找满足条件的元素的个数

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    vector<int> a = {2, 4, 6, 8, 8, 9, 9};
    int b[2] = {8, 10};
    int c = count_if(a.begin(), a.end(), [](int i) {
        return i % 2 == 0;
    });
    cout << c;
    return 0;
}

mismatch()

查找两个序列第一个不匹配的位置

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    vector<int> a = {2, 4, 6, 8, 8, 9, 9};
    int b[4] = {2, 4, 7, 8};
    pair<vector<int>::iterator, int*> p = mismatch(a.begin(), a.end(), b);
    cout << *p.first<<" ";
    cout << *p.second;
    return 0;
}

返回结果

6 7

equal()

判断范围内是否相等

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    vector<int> a = {2, 4, 6, 8, 8, 9, 9};
    int b[4] = {2, 4, 6, 8};
    bool c = equal(a.begin(), a.begin() + 4, b, b + 4);
    cout << c;
    return 0;
}

is_permutation()

判断一个序列是否是另一个序列的排列

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    vector<int> a = {2, 4, 6, 8};
    int b[4] = {4, 2, 8, 6};
    bool c = is_permutation(a.begin(), a.end(), b);
    cout << c;
    return 0;
}

搜索子序列

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    vector<int> a = {2, 4, 6, 8};
    int b[2] = {4, 6};
    auto it = search(a.begin(), a.end(), b, b + 2);
    cout << *it;
    return 0;
}

search_n()

搜索若干个值

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


using namespace std;

int main() {
    vector<int> a = {2, 4, 6, 6};
    auto it = search_n(a.begin(), a.end(), 2, 6);
    cout << *it;
    return 0;
}

??正文结束??
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值