# ===== CodeLab: cpp-stl-algorithm =====
# 以下代码片段按文章出现顺序拼接, 共 8 段

# ----- 片段 1 (cpp) -----
#include <iostream>
#include <vector>

int main() {
    std::vector<int> scores{85, 92, 78, 60, 88};
    // 手写:找最高分
    int max_score = scores[0];
    for (std::size_t i = 1; i < scores.size(); ++i) {
        if (scores[i] > max_score) max_score = scores[i];
    }
    // 手写:数及格人数
    int pass = 0;
    for (int s : scores) if (s >= 60) ++pass;
    std::cout << "最高分 " << max_score << ",及格 " << pass << " 人\n";
    return 0;
}

# ----- 片段 2 (cpp) -----
#include <iostream>

int main() {
    int base = 10;
    // [base] 按值捕获:lambda 内部是 base 的副本
    auto add_base = [base](int x) { return x + base; };
    // [&base] 按引用捕获:可以修改外部变量
    auto bump = [&base]() { base += 100; };

    std::cout << add_base(5) << "\n";  // 15
    bump();
    std::cout << base << "\n";          // 110
    // [=] 全部按值捕获,[&] 全部按引用捕获,按需使用
    auto all = [=]() { return base + 1; };
    std::cout << all() << "\n";          // 111
    return 0;
}

# ----- 片段 3 (cpp) -----
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

struct Student {
    std::string name;
    int score;
};

int main() {
    std::vector<Student> students{
        {"张三", 85}, {"李四", 92}, {"王五", 78}, {"赵六", 92}};
    // 按成绩降序
    std::sort(students.begin(), students.end(),
              [](const Student& a, const Student& b) {
                  return a.score > b.score;
              });
    // 成绩相同按姓名升序:stable_sort 保证相等元素的相对顺序
    std::stable_sort(students.begin(), students.end(),
                     [](const Student& a, const Student& b) {
                         if (a.score != b.score) return a.score > b.score;
                         return a.name < b.name;
                     });
    for (const auto& s : students)
        std::cout << s.name << " " << s.score << "\n";
    return 0;
}

# ----- 片段 4 (cpp) -----
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> names{"张三", "李四", "王五", "李四"};
    auto it = std::find(names.begin(), names.end(), "李四");
    if (it != names.end())
        std::cout << "找到: " << *it << "\n";

    // find_if:找第一个长度大于 2 的名字
    auto it2 = std::find_if(names.begin(), names.end(),
                            [](const std::string& s) { return s.size() > 2; });
    if (it2 != names.end())
        std::cout << "第一个超过 2 字的名字: " << *it2 << "\n";

    // 想数"所有"匹配的,配合循环推进起点
    std::size_t count = 0;
    for (auto p = names.begin(); p != names.end();) {
        p = std::find(p, names.end(), "李四");
        if (p == names.end()) break;
        ++count;
        ++p;
    }
    std::cout << "\"李四\" 出现 " << count << " 次\n";
    return 0;
}

# ----- 片段 5 (cpp) -----
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> scores{85, 92, 78, 60, 88};
    // for_each:打印每个分数
    std::for_each(scores.begin(), scores.end(),
                  [](int s) { std::cout << s << " "; });
    std::cout << "\n";

    // transform:每个分数 +5,存入新容器
    std::vector<int> adjusted(scores.size());
    std::transform(scores.begin(), scores.end(), adjusted.begin(),
                   [](int s) { return s + 5; });

    // 原地变换:目标起点写成源起点即可
    std::transform(scores.begin(), scores.end(), scores.begin(),
                   [](int s) { return s * 2; });
    for (int s : adjusted) std::cout << s << " ";
    std::cout << "| ";
    for (int s : scores) std::cout << s << " ";
    std::cout << "\n";
    return 0;
}

# ----- 片段 6 (cpp) -----
#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

int main() {
    std::vector<int> scores{85, 92, 78, 60, 88};
    int pass = std::count_if(scores.begin(), scores.end(),
                             [](int s) { return s >= 60; });
    int total = std::accumulate(scores.begin(), scores.end(), 0);
    double avg = static_cast<double>(total) / scores.size();
    std::cout << "及格 " << pass << " 人,平均分 " << avg << "\n";

    // accumulate 的第四参可以自定义"怎么加"
    std::string joined = std::accumulate(
        scores.begin(), scores.end(), std::string(),
        [](const std::string& acc, int s) {
            return acc + (acc.empty() ? "" : ",") + std::to_string(s);
        });
    std::cout << joined << "\n";   // 85,92,78,60,88
    return 0;
}

# ----- 片段 7 (cpp) -----
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> scores{85, 92, 78, 60, 88};
    auto max_it = std::max_element(scores.begin(), scores.end());
    auto min_it = std::min_element(scores.begin(), scores.end());
    std::cout << "最高 " << *max_it << ",最低 " << *min_it << "\n";

    // 区间判断:全部及格?有人上 90?没人低于 50?
    bool all_pass = std::all_of(scores.begin(), scores.end(),
                                [](int s) { return s >= 60; });
    bool any_high = std::any_of(scores.begin(), scores.end(),
                                [](int s) { return s >= 90; });
    bool none_low = std::none_of(scores.begin(), scores.end(),
                                 [](int s) { return s < 50; });
    std::cout << all_pass << " " << any_high << " " << none_low
              << "\n";   // 1 1 1

    // 二分查找:只对有序区间有效,O(log n)
    std::sort(scores.begin(), scores.end());
    bool found = std::binary_search(scores.begin(), scores.end(), 78);
    std::cout << "78 是否存在: " << found << "\n";   // 1
    return 0;
}

# ----- 片段 8 (cpp) -----
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> scores{85, 92, 78, 60, 88, 45, 59};
    // 删除所有不及格的
    auto new_end = std::remove_if(scores.begin(), scores.end(),
                                  [](int s) { return s < 60; });
    scores.erase(new_end, scores.end());   // 真正缩短容器

    std::cout << "剩余 " << scores.size() << " 人:";
    for (int s : scores) std::cout << " " << s;
    std::cout << "\n";   // 剩余 5 人: 85 92 78 60 88
    return 0;
}
