C++ algorithm头文件全解析

常用函数介绍

1. 最值与绝对值函数

在<algorithm>头文件中,max()和min()函数用于获取两个数中的最大值和最小值 ,abs()函数用于计算整数的绝对值。其基本语法如下:


#include <iostream>

#include <algorithm>

int main() {

int num1 = 10, num2 = 20;

// 获取num1和num2中的最大值

int max_num = std::max(num1, num2);

// 获取num1和num2中的最小值

int min_num = std::min(num1, num2);

int abs_num = std::abs(num1 - num2); // 计算num1与num2差值的绝对值

std::cout << "最大值: " << max_num << std::endl;

std::cout << "最小值: " << min_num << std::endl;

std::cout << "绝对值: " << abs_num << std::endl;

return 0;

}

上述代码中,通过std::max()、std::min()和std::abs()函数分别求出了两个整数的最大值、最小值和差值的绝对值,并将结果输出 。这几个函数不仅适用于int类型,还可以用于double、float等数值类型 。

2. 元素交换函数

swap()函数的功能是交换两个变量的值。在实际编程中,当需要交换两个变量的值时,无需手动编写交换代码,直接使用swap()函数即可。例如:


#include <iostream>

#include <algorithm>

int main() {

int a = 5, b = 10;

std::cout << "交换前: a = " << a << ", b = " << b << std::endl;

// 交换a和b的值

std::swap(a, b);

std::cout << "交换后: a = " << a << ", b = " << b << std::endl;

return 0;

}

在上述示例中,std::swap(a, b)语句将变量a和b的值进行了交换,使得原本a的值变为10,b的值变为5。swap()函数在许多算法和数据处理场景中都有广泛应用,如排序算法中的元素交换操作。

3. 元素反转函数

reverse()函数可以对数组或容器中的元素进行反转。其原理是通过迭代器从两端向中间遍历,依次交换对应位置的元素,从而实现元素顺序的反转。下面以数组和vector容器为例进行演示:


#include <iostream>

#include <algorithm>

#include <vector>

int main() {

int arr[] = {1, 2, 3, 4, 5};

int n = sizeof(arr) / sizeof(arr[0]);

std::cout << "数组反转前: ";

for (int i = 0; i < n; ++i) {

std::cout << arr[i] << " ";

}

std::cout << std::endl;

// 反转数组元素

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

std::cout << "数组反转后: ";

for (int i = 0; i < n; ++i) {

std::cout << arr[i] << " ";

}

std::cout << std::endl;

std::vector<int> vec = {6, 7, 8, 9, 10};

std::cout << "vector反转前: ";

for (auto num : vec) {

std::cout << num << " ";

}

std::cout << std::endl;

// 反转vector元素

std::reverse(vec.begin(), vec.end());

std::cout << "vector反转后: ";

for (auto num : vec) {

std::cout << num << " ";

}

std::cout << std::endl;

return 0;

}

上述代码中,分别对数组arr和vector容器vec进行了反转操作,并输出了反转前后的元素。通过std::reverse()函数,实现了对不同数据结构中元素顺序的快速调整。

4. 全排列生成函数

next_permutation()函数用于生成下一个全排列。它的工作机制是:从当前序列的末尾开始,寻找最后一个顺序对(即前一个元素小于后一个元素的对),然后在该顺序对之后,从后向前寻找第一个大于顺序对中前一个元素的元素,将这两个元素交换,最后将交换后的顺序对之后的元素反转,得到下一个字典序更大的排列。当已经是最后一个排列时,函数会将序列变为第一个排列,并返回false;否则返回true。以下是使用next_permutation()函数输出数组所有全排列的代码示例:


#include <iostream>

#include <algorithm>

int main() {

int arr[] = {1, 2, 3};

int n = sizeof(arr) / sizeof(arr[0]);

// 首先对数组进行排序,以得到最小的排列

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

do {

std::cout << "当前排列: ";

for (int i = 0; i < n; ++i) {

std::cout << arr[i] << " ";

}

std::cout << std::endl;

} while (std::next_permutation(arr, arr + n));

return 0;

}

在上述代码中,通过std::sort()函数对数组进行排序,然后使用do-while循环结合std::next_permutation()函数,依次输出数组的所有全排列。每一次循环,std::next_permutation()函数都会将数组调整为下一个全排列,直到所有全排列都被输出。

5. 元素填充函数

fill()函数用于将数组或容器中的特定区间元素赋为相同的值。其基本用法是指定要填充的起始位置和结束位置,以及要填充的值。例如,将数组的一段区间填充为指定值:


#include <iostream>

#include <algorithm>

int main() {

int arr[10] = {0};

// 将arr数组从下标0到下标4的元素填充为5

std::fill(arr, arr + 5, 5);

std::cout << "填充后的数组: ";

for (int i = 0; i < 10; ++i) {

std::cout << arr[i] << " ";

}

std::cout << std::endl;

return 0;

}

在上述代码中,std::fill(arr, arr + 5, 5)语句将数组arr的前 5 个元素都填充为 5,从而实现了对数组特定区间元素的批量赋值操作。

6. 排序函数

6.1 基本用法

sort()函数是<algorithm>头文件中用于排序的核心函数,它可以对数组和各种容器进行排序。其基本语法为sort(begin, end),其中begin指向要排序的起始位置,end指向要排序的结束位置的下一个位置(即左闭右开区间)。以下是对整型数组进行默认升序排序的示例:


#include <iostream>

#include <algorithm>

int main() {

int arr[] = {5, 3, 8, 1, 9, 2};

int n = sizeof(arr) / sizeof(arr[0]);

std::cout << "排序前的数组: ";

for (int i = 0; i < n; ++i) {

std::cout << arr[i] << " ";

}

std::cout << std::endl;

// 对数组进行默认升序排序

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

std::cout << "排序后的数组: ";

for (int i = 0; i < n; ++i) {

std::cout << arr[i] << " ";

}

std::cout << std::endl;

return 0;

}

在上述代码中,std::sort(arr, arr + n)函数调用将数组arr中的元素按照升序进行排列,排序后的数组元素从小到大依次输出。

6.2 自定义比较函数

在实际应用中,有时需要按照特定的规则进行排序,这时就需要编写自定义比较函数cmp。cmp函数是一个返回布尔值的函数,用于定义两个元素之间的比较关系。如果要实现从大到小排序,可以编写如下的自定义比较函数:


#include <iostream>

#include <algorithm>

// 自定义比较函数,实现从大到小排序

bool cmp(int a, int b) {

return a > b;

}

int main() {

int arr[] = {5, 3, 8, 1, 9, 2};

int n = sizeof(arr) / sizeof(arr[0]);

std::cout << "排序前的数组: ";

for (int i = 0; i < n; ++i) {

std::cout << arr[i] << " ";

}

std::cout << std::endl;

// 使用自定义比较函数进行从大到小排序

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

std::cout << "排序后的数组: ";

for (int i = 0; i < n; ++i) {

std::cout << arr[i] << " ";

}

std::cout << std::endl;

return 0;

}

上述代码中,cmp函数定义了a > b的比较规则,std::sort(arr, arr + n, cmp)调用使用这个自定义比较函数对数组进行从大到小的排序。

除了基本数据类型,自定义比较函数还可以用于结构体数组的排序。例如,假设有一个表示学生的结构体Student,包含姓名和成绩两个成员,要按照成绩从高到低对学生进行排序,可以这样实现:


#include <iostream>

#include <algorithm>

#include <string>

struct Student {

std::string name;

int score;

};

// 自定义比较函数,按照成绩从高到低排序

bool cmpStudent(const Student& a, const Student& b) {

return a.score > b.score;

}

int main() {

Student students[] = {{"Alice", 85}, {"Bob", 90}, {"Charlie", 78}};

int n = sizeof(students) / sizeof(students[0]);

std::cout << "排序前的学生列表:" << std::endl;

for (int i = 0; i < n; ++i) {

std::cout << "姓名: " << students[i].name << ", 成绩: " << students[i].score << std::endl;

}

// 使用自定义比较函数对结构体数组进行排序

std::sort(students, students + n, cmpStudent);

std::cout << "排序后的学生列表:" << std::endl;

for (int i = 0; i < n; ++i) {

std::cout << "姓名: " << students[i].name << ", 成绩: " << students[i].score << std::endl;

}

return 0;

}

在这个示例中,cmpStudent函数定义了按照学生成绩从高到低的比较规则,std::sort(students, students + n, cmpStudent)调用使用这个规则对students结构体数组进行排序,从而实现了按照特定规则对复杂数据类型的排序操作。

7. 二分查找函数

7.1 lower_bound () 函数

lower_bound()函数用于在有序数组或容器中查找第一个大于等于指定值的元素位置。它采用二分查找的思想,在有序序列中快速定位目标元素或其合适的插入位置。例如:


#include <iostream>

#include <algorithm>

#include <vector>

int main() {

int arr[] = {1, 3, 5, 7, 9, 11};

int n = sizeof(arr) / sizeof(arr[0]);

int target = 6;

// 在数组中查找第一个大于等于target的元素位置

int* result = std::lower_bound(arr, arr + n, target);

if (result!= arr + n) {

std::cout << "第一个大于等于 " << target << " 的元素是: " << *result << std::endl;

} else {

std::cout << "没有找到大于等于 " << target << " 的元素" << std::endl;

}

std::vector<int> vec = {2, 4, 6, 8, 10};

auto it = std::lower_bound(vec.begin(), vec.end(), 7);

if (it!= vec.end()) {

std::cout << "在vector中,第一个大于等于 7 的元素是: " << *it << std::endl;

} else {

std::cout << "在vector中,没有找到大于等于 7 的元素" << std::endl;

}

return 0;

}

在上述代码中,首先在数组arr中使用std::lower_bound()函数查找第一个大于等于6的元素位置,然后在vector容器vec中查找第一个大于等于7的元素位置,并输出相应的结果。如果找到符合条件的元素,则输出该元素的值;否则,输出提示信息。

7.2 upper_bound () 函数

upper_bound()函数的作用是在有序数组或容器中查找第一个大于指定值的元素位置。它同样基于二分查找算法,在有序序列中高效地定位目标位置。以下是使用upper_bound()函数的示例:


#include <iostream>

#include <algorithm>

#include <vector>

int main() {

int arr[] = {1, 3, 5, 7, 9, 11};

int n = sizeof(arr) / sizeof(arr[0]);

int target = 6;

// 在数组中查找第一个大于target的元素位置

int* result = std::upper_bound(arr, arr + n, target);

if (result!= arr + n) {

std::cout << "第一个大于 " << target << " 的元素是: " << *result << std::endl;

} else {

std::cout << "没有找到大于 " << target << " 的元素" << std::endl;

}

std::vector<int> vec = {2, 4, 6, 8, 10};

auto it = std::upper_bound(vec.begin(), vec.end(), 7);

if (it!= vec.end()) {

std::cout << "在vector中,第一个大于 7 的元素是: " << *it << std::endl;

} else {

std::cout << "在vector中,没有找到大于 7 的元素" << std::endl;

}

return 0;

}

在这段代码中,分别在数组arr和vector容器vec中使用std::upper_bound()函数查找第一个大于6和7的元素位置,并根据查找结果输出相应信息。通过upper_bound()函数,可以方便地在有序数据结构中确定特定元素的上界位置,这在许多算法和数据处理场景中都非常有用,如查找插入位置、统计满足条件的元素个数等。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值