C++之常用通用算法

背景
C++ 是一种强大的编程语言,它提供了许多通用算法,可以用于各种容器类型。这些算法是通过迭代器来操作容器中的元素,因此它们是通用的,可以用于不同类型的容器。在本篇博客中,我们将详细介绍 C++ 的通用算法。

1、std::sort()
std::sort() 是 C++ 中最常用的算法之一,它可以对容器中的元素进行排序。该算法使用快速排序算法实现,时间复杂度为 O(n log n)。以下是一个使用 std::sort() 对数组进行排序的示例:

#include <algorithm>
#include <iostream>

int main() {
    int arr[] = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);

    std::sort(arr, arr + n);

    for (int i = 0; i < n; i++) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}

使用 std::sort() 对数组 arr 进行排序,并使用循环打印排序后的结果。

2、std::find()
std::find() 可以在容器中查找指定的元素。该算法返回一个迭代器,指向第一个匹配的元素。如果没有找到匹配的元素,则返回容器的 end() 迭代器。以下是一个使用 std::find() 查找向量中的元素的示例:

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

int main() {
    std::vector<int> vec = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3 };
    int x = 5;

    auto it = std::find(vec.begin(), vec.end(), x);

    if (it != vec.end()) {
        std::cout << "Found " << x << " at position " << it - vec.begin() << std::endl;
    } else {
        std::cout << "Not found" << std::endl;
    }

    return 0;
}

在上面的示例中,我们使用 std::find() 在向量 vec 中查找元素 x,并打印结果。

3、std::accumulate()
std::accumulate() 可以计算容器中元素的总和。该算法需要两个迭代器和一个初始值作为参数。以下是一个使用 std::accumulate() 计算数组元素总和的示例:

#include <algorithm>
#include <iostream>

int main() {
    int arr[] = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);

    int sum = std::accumulate(arr, arr + n, 0);

    std::cout << "Sum: " << sum << std::endl;

    return 0;
}

在上面的示例中,我们使用 std::accumulate() 计算数组 arr 中元素的总和,并打印结果。

4、std::transform()
std::transform() 可以对容器中的元素进行转换。该算法需要两个迭代器和一个转换函数作为参数。以下是一个使用 std::transform() 将数组中的元素乘以 2 的示例:

#include <algorithm>
#include <iostream>

int main() {
    int arr[] = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);

    std::transform(arr, arr + n, arr, [](int x) { return x * 2; });

    for (int i = 0; i < n; i++) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}

在上面的示例中,我们使用 std::transform() 将数组 arr 中的元素乘以 2,并使用循环打印结果。

5、std::copy()
std::copy() 可以将容器中的元素复制到另一个容器中。该算法需要两个迭代器和一个目标容器的迭代器作为参数。以下是一个使用 std::copy() 将数组中的元素复制到向量中的示例:

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

int main() {
    int arr[] = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);

    std::vector<int> vec(n);
    std::copy(arr, arr + n, vec.begin());

    for (int i = 0; i < n; i++) {
        std::cout << vec[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}

以上使用 std::copy() 将数组 arr 中的元素复制到向量 vec 中,并使用循环打印结果。

6、std::reverse()
std::reverse() 可以反转容器中的元素顺序。该算法需要两个迭代器作为参数。以下是一个使用 std::reverse() 反转数组中的元素顺序的示例:

#include <algorithm>
#include <iostream>

int main() {
    int arr[] = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);

    std::reverse(arr, arr + n);

    for (int i = 0; i < n; i++) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}

在上面的示例中,我们使用 std::reverse() 反转数组 arr 中的元素顺序,并使用循环打印结果。

7、std::unique()
std::unique() 可以从容器中删除重复的元素。该算法需要两个迭代器作为参数,并返回一个迭代器,指向不重复的元素的末尾。以下是一个使用 std::unique() 删除向量中重复元素的示例:

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

int main() {
    std::vector<int> vec = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3 };
    std::sort(vec.begin(), vec.end());

    auto it = std::unique(vec.begin(), vec.end());
    vec.erase(it, vec.end());

    for (int i = 0; i < vec.size(); i++) {
        std::cout << vec[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}

在上面的示例中,我们使用 std::unique() 删除向量 vec 中的重复元素,并使用循环打印结果。

8、std::for_each()
std::for_each() 可以对容器中的每个元素执行指定的操作。该算法需要两个迭代器和一个函数对象作为参数。以下是一个使用 std::for_each() 对数组中的元素进行平方的示例:

#include <algorithm>
#include <iostream>

int main() {
    int arr[] = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);

    std::for_each(arr, arr + n, [](int& x) { x *= x; });

    for (int i = 0; i < n; i++) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}

在上面的示例中,我们使用 std::for_each() 对数组 arr 中的元素进行平方,并使用循环打印结果。

最后:
C++ 的通用算法可以大大简化编写 C++ 程序的过程,因为它们提供了一种通用的方法来处理容器中的元素,而不需要编写特定于容器类型的代码。此外,这些算法已经经过优化,因此它们通常比手动编写的代码更快、更可靠。在实际编程中,我们应该熟练掌握这些算法,并根据需要选择合适的算法来处理容器中的元素。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ghx3110

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值