C++中sort,upper_bound,lower_bound,*max_element,unique用法介绍

本文详细介绍了C++标准模板库(STL)中的排序与搜索算法,包括sort函数的基本使用、max_element获取最大值的方法、unique去重操作、upper_bound与lower_bound的二分查找技巧等。

1.sort:sort(fast,last,排序方式),sort排序函数是对[fast,last)前开后闭区间内元素进行排序,fast是数组首地址,last是数组尾地址的下一个地址。如sort(a,a+n,cmp),

如果不加排序方式则是默认由小到大排序。

2.*max_element:*max_element(fast,last)该函数也是对[fast,last)前闭后开区间内进行求数组中最大值的操作。

3.unique:unique(fast,last)函数是对[fast ,last)区间内去除相邻的重复元素,往往要先对数组排序,在头文件#include<algorithm>目录下。且并不是把重复元素删除了,而是把重复的元素排在数组后面,返回值是去重后最后一个元素的尾地址,例如:

a[6]={1,2,2,3,3,5};那么unique(a,a+6)-a的值就是4,而不是3,区别于本文其他的函数返回值。

4.upper_bound:upper_bound(fast,last,n)是[fast,last)区间用二分查找找到第一个大于n的值,返回值为第一个大于n的值的地址,例如:upper_bound(a,a+n,k)-a的值的实际上就是第一个大于k的元素的下标,如果n大于数组中的全部元素,则返回last。

5.lower_bound:lower_bound(fast,last,n)是[fast,last)区间二分查找第一个大于等于n的值,返回值为第一个大于等于n的值的地址,其他同upper_bound函数。

如有错误希望大家能提醒一下,在下马上改正。微笑

#include <iostream> #include <vector> #include <algorithm> #include <functional> // std::greater #include <string> #include <cctype> int main() { std::vector<int> vec = {5, 2, 8, 2, 9, 1, 5, 5}; std::vector<int> vec2(8); std::vector<std::string> words = {"apple", "Banana", "cherry"}; // 1. sort 排序 std::sort(vec.begin(), vec.end()); std::cout << "Sorted: "; for (int x : vec) std::cout << x << " "; std::cout << "\n"; // 2. reverse 反转 std::reverse(vec.begin(), vec.end()); std::cout << "Reversed: "; for (int x : vec) std::cout << x << " "; std::cout << "\n"; // 3. find 查找 auto it = std::find(vec.begin(), vec.end(), 8); if (it != vec.end()) { std::cout << "Found 8 at index: " << std::distance(vec.begin(), it) << "\n"; } // 4. count 统计 int cnt = std::count(vec.begin(), vec.end(), 5); std::cout << "Count of 5: " << cnt << "\n"; // 5. transform 转换(例如平方) std::transform(vec.begin(), vec.end(), vec2.begin(), [](int x) { return x * x; }); std::cout << "Transformed (squared): "; for (int x : vec2) std::cout << x << " "; std::cout << "\n"; // 6. replace 替换 std::replace(vec2.begin(), vec2.end(), 25, 100); // 把所有25换成100 std::cout << "After replace 25 -> 100: "; for (int x : vec2) std::cout << x << " "; std::cout << "\n"; // 7. unique 去除相邻重复(必须先排序) std::sort(vec.begin(), vec.end()); auto new_end = std::unique(vec.begin(), vec.end()); vec.erase(new_end, vec.end()); // 实际删除 std::cout << "After unique: "; for (int x : vec) std::cout << x << " "; std::cout << "\n"; // 8. min_element / max_element auto min_it = std::min_element(vec.begin(), vec.end()); auto max_it = std::max_element(vec.begin(), vec.end()); std::cout << "Min: " << *min_it << ", Max: " << *max_it << "\n"; // 9. binary_search (需要有序) bool found = std::binary_search(vec.begin(), vec.end(), 8); std::cout << "Binary search for 8: " << (found ? "found" : "not found") << "\n"; // 10. lower_bound / upper_bound auto lb = std::lower_bound(vec.begin(), vec.end(), 5); auto ub = std::upper_bound(vec.begin(), vec.end(), 5); std::cout << "Lower bound of 5 at index: " << std::distance(vec.begin(), lb) << "\n"; std::cout << "Upper bound of 5 at index: " << std::distance(vec.begin(), ub) << "\n"; // 11. next_permutation std::string s = "abc"; do { std::cout << s << " "; } while (std::next_permutation(s.begin(), s.end())); std::cout << "\n"; // 12. copy std::vector<int> copy_vec(vec.size()); std::copy(vec.begin(), vec.end(), copy_vec.begin()); std::cout << "Copy: "; for (int x : copy_vec) std::cout << x << " "; std::cout << "\n"; // 13. fill std::fill(vec2.begin(), vec2.end(), 0); std::cout << "After fill with 0: "; for (int x : vec2) std::cout << x << " "; std::cout << "\n"; // 14. equal 判断两个范围是否相同 bool same = std::equal(vec.begin(), vec.end(), copy_vec.begin()); std::cout << "vec and copy_vec are " << (same ? "equal" : "not equal") << "\n"; // 15. 自定义比较函数(降序排序) std::sort(copy_vec.begin(), copy_vec.end(), std::greater<int>()); std::cout << "Descending order: "; for (int x : copy_vec) std::cout << x << " "; std::cout << "\n"; // 16. find_if 使用谓词 auto even_it = std::find_if(copy_vec.begin(), copy_vec.end(), [](int x) { return x % 2 == 0; }); if (even_it != copy_vec.end()) { std::cout << "First even number: " << *even_it << "\n"; } // 17. count_if int even_count = std::count_if(copy_vec.begin(), copy_vec.end(), [](int x) { return x % 2 == 0; }); std::cout << "Number of even numbers: " << even_count << "\n"; return 0; } 代码中需要有简要注释函数功能
10-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值