编译运行:clang++ -std=c++26 -Wall -Wextra -pthread 测试_第14章_STL算法.cpp -o t && ./t(需先 cd 测试/)
// 第14章测试:STL 算法
#include <print>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <cctype>
int failures = 0;
#define CHECK(expr) \
do { \
if (!(expr)) { \
++failures; \
std::println("FAIL 第{}行: {}", __LINE__, #expr); \
} \
} while (0)
struct Student {
std::string name;
int score;
};
int main() {
// 14.2 查询类
std::vector<int> v{5, 3, 1, 4, 2, 4};
CHECK(*std::max_element(v.begin(), v.end()) == 5);
CHECK(*std::min_element(v.begin(), v.end()) == 1);
CHECK(std::count(v.begin(), v.end(), 4) == 2);
CHECK(std::count_if(v.begin(), v.end(), [](int x) { return x % 2 == 0; }) == 3);
CHECK(std::all_of(v.begin(), v.end(), [](int x) { return x > 0; }));
CHECK(std::any_of(v.begin(), v.end(), [](int x) { return x == 5; }));
CHECK(std::none_of(v.begin(), v.end(), [](int x) { return x > 100; }));
auto it = std::find_if(v.begin(), v.end(), [](int x) { return x > 4; });
CHECK(it != v.end() && *it == 5);
CHECK(std::find(v.begin(), v.end(), 99) == v.end());
// 排序后二分查找
std::vector<int> sorted = v;
std::sort(sorted.begin(), sorted.end());
CHECK(std::binary_search(sorted.begin(), sorted.end(), 4));
// 14.3 修改类
std::vector<int> rev{1, 2, 3, 4, 5};
std::reverse(rev.begin(), rev.end());
CHECK(rev[0] == 5);
std::fill(rev.begin(), rev.end(), 0);
CHECK(std::all_of(rev.begin(), rev.end(), [](int x) { return x == 0; }));
std::replace(rev.begin(), rev.end(), 0, 7);
CHECK(std::all_of(rev.begin(), rev.end(), [](int x) { return x == 7; }));
// 组合拳:去重
std::vector<int> dup{3, 1, 3, 2, 1, 3, 4};
std::sort(dup.begin(), dup.end());
dup.erase(std::unique(dup.begin(), dup.end()), dup.end());
CHECK((dup == std::vector<int>{1, 2, 3, 4}));
// 组合拳:删除所有偶数
std::vector<int> del{1, 2, 3, 4, 5, 6};
del.erase(std::remove_if(del.begin(), del.end(),
[](int x) { return x % 2 == 0; }),
del.end());
CHECK((del == std::vector<int>{1, 3, 5}));
// 14.4 变换与生成
std::vector<int> src{1, 2, 3};
std::vector<int> dst(3);
std::transform(src.begin(), src.end(), dst.begin(),
[](int x) { return x * x; });
CHECK((dst == std::vector<int>{1, 4, 9}));
CHECK(std::accumulate(src.begin(), src.end(), 0) == 6);
CHECK(std::accumulate(src.begin(), src.end(), 1,
[](int a, int b) { return a * b; }) == 6);
std::vector<int> seq(5);
std::iota(seq.begin(), seq.end(), 0);
CHECK((seq == std::vector<int>{0, 1, 2, 3, 4}));
// 14.5 自定义类型排序
std::vector<Student> stu{{"张三", 70}, {"李四", 95}, {"王五", 60}};
std::sort(stu.begin(), stu.end(),
[](const Student& a, const Student& b) {
return a.score > b.score;
});
CHECK(stu[0].name == "李四");
CHECK(stu[2].name == "王五");
// 14.6 ranges 版
std::vector<int> rv{4, 2, 5, 1, 3};
std::ranges::sort(rv);
CHECK(std::ranges::is_sorted(rv));
std::ranges::sort(rv, std::greater<>{});
CHECK(rv[0] == 5);
// 投影:按 Student.score 排序(升序)
std::ranges::sort(stu, {}, &Student::score);
CHECK(stu[0].name == "王五");
CHECK(std::ranges::max_element(stu, {}, &Student::score)->name == "李四");
// 14.7 汇总
std::vector<int> vals{1, 2, 3, 4, 5};
int even_count = std::ranges::count_if(vals, [](int x) { return x % 2 == 0; });
CHECK(even_count == 2);
CHECK(std::ranges::all_of(vals, [](int x) { return x >= 1; }));
// 14.8 大小写转换
std::string text = "Hello";
std::transform(text.begin(), text.end(), text.begin(),
[](unsigned char c) { return std::toupper(c); });
CHECK(text == "HELLO");
if (failures == 0) std::println("全部通过");
return failures;
}