C++基础 | Algorithm库

Algorithm库概述

<algorithm>库中的算法主要分为以下几类:

  1. 非修改序列操作:对序列进行遍历、查找、计数等操作,但不修改序列中的元素。
  2. 修改序列操作:对序列中的元素进行复制、替换、删除、旋转等操作。
  3. 排序和相关操作:对序列进行排序、合并、二分查找等操作。
  4. 数值操作:对序列中的元素进行数值计算,如累加、内积等。

常用算法详解

1. 非修改序列操作

std::for_each

std::for_each用于对序列中的每个元素执行指定的操作。

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

void print(int n) {
    std::cout << n << " ";
}

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};
    std::for_each(vec.begin(), vec.end(), print); // 输出:1 2 3 4 5
    return 0;
}
std::find

std::find用于在序列中查找指定的值,返回指向该元素的迭代器。如果未找到,则返回end()迭代器。

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

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};
    auto it = std::find(vec.begin(), vec.end(), 3);
    if (it != vec.end()) {
        std::cout << "Found: " << *it << '\n'; // 输出:Found: 3
    } else {
        std::cout << "Not found\n";
    }
    return 0;
}

2. 修改序列操作

std::copy

std::copy用于将一个序列中的元素复制到另一个序列中。

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

int main() {
    std::vector<int> src = {1, 2, 3, 4, 5};
    std::vector<int> dst(5);
    std::copy(src.begin(), src.end(), dst.begin());
    for (int n : dst) {
        std::cout << n << " "; // 输出:1 2 3 4 5
    }
    return 0;
}
std::replace

std::replace用于将序列中所有等于指定值的元素替换为另一个值。

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

int main() {
    std::vector<int> vec = {1, 2, 3, 2, 5};
    std::replace(vec.begin(), vec.end(), 2, 99);
    for (int n : vec) {
        std::cout << n << " "; // 输出:1 99 3 99 5
    }
    return 0;
}

3. 排序和相关操作

std::sort

std::sort用于对序列中的元素进行排序。

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

int main() {
    std::vector<int> vec = {5, 3, 1, 4, 2};
    std::sort(vec.begin(), vec.end());
    for (int n : vec) {
        std::cout << n << " "; // 输出:1 2 3 4 5
    }
    return 0;
}
std::binary_search

std::binary_search用于在已排序的序列中进行二分查找。

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

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};
    bool found = std::binary_search(vec.begin(), vec.end(), 3);
    std::cout << "Found: " << std::boolalpha << found << '\n'; // 输出:Found: true
    return 0;
}

4. 数值操作

std::accumulate

std::accumulate用于计算序列中元素的累加和。

#include <iostream>
#include <vector>
#include <numeric>

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};
    int sum = std::accumulate(vec.begin(), vec.end(), 0);
    std::cout << "Sum: " << sum << '\n'; // 输出:Sum: 15
    return 0;
}
std::inner_product

std::inner_product用于计算两个序列的内积。

#include <iostream>
#include <vector>
#include <numeric>

int main() {
    std::vector<int> vec1 = {1, 2, 3};
    std::vector<int> vec2 = {4, 5, 6};
    int result = std::inner_product(vec1.begin(), vec1.end(), vec2.begin(), 0);
    std::cout << "Inner product: " << result << '\n'; // 输出:Inner product: 32
    return 0;
}

实际应用示例

示例1:自定义排序

我们可以通过传递自定义的比较函数来对序列进行自定义排序。

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

bool compare(int a, int b) {
    return a > b; // 降序排序
}

int main() {
    std::vector<int> vec = {5, 3, 1, 4, 2};
    std::sort(vec.begin(), vec.end(), compare);
    for (int n : vec) {
        std::cout << n << " "; // 输出:5 4 3 2 1
    }
    return 0;
}

示例2:删除特定元素

我们可以使用std::remove_ifstd::erase来删除序列中满足特定条件的元素。

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

bool is_odd(int n) {
    return n % 2 != 0;
}

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};
    vec.erase(std::remove_if(vec.begin(), vec.end(), is_odd), vec.end());
    for (int n : vec) {
        std::cout << n << " "; // 输出:2 4
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值