Let’s Make C++ Great Again——algorithm库

algorithm

在 C++ 中,Algorithm 是一个标准库,它提供了许多通用的算法函数,可以用于对容器(如数组、vector、list 等)中的元素进行操作和处理。Algorithm 库包含了许多常用的算法,如排序、查找、合并和删除等。

以下是一些常用的 Algorithm 库函数:

sort()

sort() 函数可以对容器中的元素进行排序,使用快速排序算法或堆排序算法。例如:

vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3};
sort(v.begin(), v.end());  // 对 v 中的元素进行排序

find()

find() 函数可以在容器中查找指定元素的位置,返回该元素在容器中的迭代器。例如:

vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3};
auto it = find(v.begin(), v.end(), 5);  // 查找 5 在 v 中的位置

reverse()

reverse() 函数可以将容器中的元素反转,使第一个元素变为最后一个,最后一个元素变为第一个。例如:

vector<int> v = {1, 2, 3, 4, 5};
reverse(v.begin(), v.end());  // 反转 v 中的元素

copy()

copy() 函数可以将容器中的元素复制到另一个容器中。例如:

vector<int> v1 = {1, 2, 3, 4, 5};
vector<int> v2(5);  // 创建一个大小为 5 的 vector
copy(v1.begin(), v1.end(), v2.begin());  // 将 v1 中的元素复制到 v2 中

accumulate()

accumulate() 函数可以对容器中的元素进行累加操作,返回累加结果。例如:

vector<int> v = {1, 2, 3, 4, 5};
int sum = accumulate(v.begin(), v.end(), 0);  // 对 v 中的元素求和

count()

count() 函数可以统计容器中指定元素的个数。例如:

vector<int> v = {1, 2, 3, 2, 5};
int num = count(v.begin(), v.end(), 2);  // 统计 v 中 2 的个数

min() 和 max()

min() 和 max() 函数可以返回两个值中的最小值和最大值。例如:

int a = 1, b = 2;
int c = min(a, b);  // c = 1
int d = max(a, b);  // d = 2

replace()

replace() 函数可以将容器中指定值的元素替换为新值。例如:

vector<int> v = {1, 2, 3, 2, 5};
replace(v.begin(), v.end(), 2, 4);  // 将 v 中所有的 2 替换为 4

unique()

unique() 函数可以将容器中相邻的重复元素去重,返回去重后的容器尾部迭代器。例如:

vector<int> v = {1, 2, 2, 3, 3, 3, 4, 4, 5};
auto it = unique(v.begin(), v.end());  // 去重
v.erase(it, v.end());  // 删除重复的元素

lower_bound() 和 upper_bound()

lower_bound() 和 upper_bound() 函数可以在有序容器中查找指定元素的位置,返回该元素在容器中的迭代器。lower_bound() 返回第一个大于等于指定元素的位置,而 upper_bound() 返回第一个大于指定元素的位置。例如:

vector<int> v = {1, 2, 3, 4, 5};
auto it1 = lower_bound(v.begin(), v.end(), 3);  // 查找 3 在 v 中的位置
auto it2 = upper_bound(v.begin(), v.end(), 3);  // 查找第一个大于 3 的元素在 v 中的位置

next_permutation() 和 prev_permutation()

next_permutation() 和 prev_permutation() 函数可以获取容器中下一个或上一个排列。它们会将容器中的元素重新排列,返回 true 表示获取成功,false 表示已经是最后一个或第一个排列。例如:

vector<int> v = {1, 2, 3};
do {
    // 处理排列
} while (next_permutation(v.begin(), v.end()));

for_each()

for_each() 函数可以遍历容器中的所有元素,并对其进行指定操作。例如:

vector<int> v = {1, 2, 3, 4, 5};
for_each(v.begin(), v.end(), [](int& n){ n *= 2; });  // 将 v 中的所有元素乘以 2

merge()

merge() 函数可以将两个已排序的容器合并为一个已排序的容器,返回合并后的容器尾部迭代器。例如:

vector<int> v1 = {1, 3, 5};
vector<int> v2 = {2, 4, 6};
vector<int> v3(v1.size() + v2.size());  // 创建一个足够大的 vector
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());  // 合并 v1 和 v2 到 v3

nth_element()

nth_element() 函数可以对容器中的元素进行部分排序,将第 n 小的元素放在第 n 个位置,前 n-1 个元素都小于等于第 n 个元素,后面的元素都大于等于第 n 个元素。例如:

vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3};
nth_element(v.begin(), v.begin() + 5, v.end());  // 将 v 中第 5 小的元素放在第 5 个位置

partial_sort()

partial_sort() 函数可以对容器中的元素进行部分排序,将前 n 个最小的元素放在容器的前 n 个位置,其余元素的顺序不保证。例如:

vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3};
partial_sort(v.begin(), v.begin() + 3, v.end());  // 将 v 中前 3 个最小的元素放在前 3 个位置

partition()

partition() 函数可以将容器中的元素分为两部分,使满足指定条件的元素在前面,不满足条件的元素在后面。例如:

vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3};
auto it = partition(v.begin(), v.end(), [](int n){ return n % 2 == 0; });  // 将 v 中的偶数放在前面,奇数放在后面

binary_search()

binary_search() 函数可以在有序容器中二分查找指定元素,返回 true 表示找到,false 表示未找到。例如:

vector<int> v = {1, 2, 3, 4, 5};
bool found = binary_search(v.begin(), v.end(), 3);  // 在 v 中查找 3 是否存在

binary_search 是 C++ STL 中的一个函数,用于在有序序列中查找指定元素。如果元素存在,则返回 true,否则返回 false。以下是 binary_search 的函数定义:

template <class ForwardIterator, class T>
bool binary_search(ForwardIterator first, ForwardIterator last, const T& val);

其中,ForwardIterator 是一种迭代器类型,表示一个前向迭代器,first 和 last 分别表示要查找的有序序列的起始位置和终止位置(注意,该序列必须已经按照升序排列),val 表示要查找的元素。

使用 binary_search 函数查找指定元素的示例代码:

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

using namespace std;

int main() {
    vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int val = 5;

    if (binary_search(v.begin(), v.end(), val)) {
        cout << "Found " << val << " in the vector." << endl;
    } else {
        cout << "Did not find " << val << " in the vector." << endl;
    }

    return 0;
}

在上面的代码中,我们首先定义了一个有序的整数序列 v,然后使用 binary_search 函数查找指定元素 val。如果查找到了该元素,则输出相应的提示信息,否则输出未找到的提示信息。

需要注意的是,binary_search 函数的前提条件是有序序列。如果序列未排序,则需要先使用 sort 函数对其进行排序,然后再使用 binary_search 函数进行查找。另外,如果序列中存在重复元素,binary_search 函数只能保证找到其中的一个元素,不能保证找到最后一个或第一个元素。如果需要查找重复元素的最后一个或第一个位置,可以使用 lower_bound 和 upper_bound 函数。

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

FeatherWaves

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

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

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

打赏作者

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

抵扣说明:

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

余额充值