#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;
}
代码中需要有简要注释函数功能