1
2
3
4
5
6
7
8
9
10
for (int i = 0; i < N; i++) {
std::cin >> A[i];
}

auto g = A;
std::ranges::sort(g);
g.erase(std::ranges::unique(g).begin(), g.end());
for (auto& x : A) {
x = std::ranges::lower_bound(g, x) - g.begin();
}

chmax

1
2
3
4
5
6
7
8
9
template<class T>
void chmax(T& x, const T& other) {
if (x < other) x = other;
}

template<class T>
void chmin(T& x, const T& other) {
if (x > other) x = other;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int N;
std::cin >> N;

std::vector<std::vector<int>> Adj(N);
for (auto _ : std::views::iota(0, N - 1)) {
int X, Y;
std::cin >> X >> Y;
X--;
Y--;
Adj[X].push_back(Y);
Adj[Y].push_back(X);
}

std::vector<int> seq {0};
std::vector<int> p(N, -1);

for (int i = 0; i < seq.size(); i++) {
int x = seq[i];
for (int y : Adj[x]) {
if (y != p[x]) {
p[y] = x;
seq.push_back(y);
}
}
}

[&](this auto&& self, int x, int p) -> void {
for (auto y : Adj[x]) {
if (y == p) {
continue;
}
self(y, x);
}
} (0, -1);

views

像 Python 一样优雅:

1
2
3
for (auto [idx, task] : tasks | std::views::enumerate) {
std::cout << " 步骤 " << idx + 1 << ": " << task << '\n';
}
1
2
3
for (auto [prev, curr, next] : temps | std::views::adjacent<3>) { 
// 自动执行 size - 2 次
}

format

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// 基础格式化 
format("Hello, {}!", "World"); // Hello, World!

// 多个参数
format("Name: {}, Age: {}", "Alice", 25); // Name: Alice, Age: 25

// 带索引的参数
format("{1} before {0}", "B", "A"); // A before B

// 整数格式化
format("{:d}", 42); // "42" (十进制)
format("{:x}", 255); // "ff" (十六进制小写)
format("{:X}", 255); // "FF" (十六进制大写)
format("{:o}", 63); // "77" (八进制)
format("{:b}", 5); // "101" (二进制)

// 宽度和对齐
format("{:10}", 42); // " 42" (右对齐)
format("{:<10}", 42); // "42 " (左对齐)
format("{:^10}", 42); // " 42 " (居中对齐)
format("{:>10}", 42); // " 42" (右对齐)

// 填充字符
format("{:*>10}", 42); // "********42"
format("{:0<10}", 42); // "4200000000"

// 精度控制
format("{:.2f}", 3.14159); // "3.14"
format("{:.5f}", 3.14); // "3.14000"

// 科学计数法
format("{:.2e}", 1234.567); // "1.23e+03"

// 自动选择格式
format("{:.2g}", 1234.567); // "1.2e+03"
format("{:.2g}", 12.3456); // "12"

string str = "hello";
format("{:10}", str); // "hello "
format("{:>10}", str); // " hello"
format("{:^10}", str); // " hello "
format("{:.3}", str); // "hel" (截断)