-
Notifications
You must be signed in to change notification settings - Fork 79
/
2.3结构化绑定.cpp
50 lines (40 loc) · 1.11 KB
/
2.3结构化绑定.cpp
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
43
44
45
46
47
48
49
50
#include<iostream>
#include<format>
#include<array>
#include<tuple>
#include<map>
template < typename... Args>
void print(const std::string_view fmt_str, Args&&... args) {
auto fmt_args{ std::make_format_args(args...) };
std::string outstr{ std::vformat(fmt_str, fmt_args) };
fputs(outstr.c_str(), stdout);
}
struct X { int a; double b; std::string str; };
auto f()->std::tuple<int,int> {
return { 1,2 };
}
int main() {
int array[]{ 1,2,3,4,5 };
auto& [a, b, c, d, e] = array;
print("{} {} {} {} {}\n", a, b, c, d, e);
a = 10;
print("{}\n", array[0]);
std::array arr{ '*','a','b','&' };
auto [a_, b_, c_, d_] = arr;
print("{} {} {} {}\n", a_, b_, c_, d_);
std::tuple<int, double, std::string>tu{ 10,3.14,"🥵" };
auto [t1, t2, t3] = tu;
print("{} {} {}\n", t1, t2, t3);
X x{ 1,5.2,"🤣" };
auto [x1, x2, x3] = x;
print("{} {} {}\n", x1, x2, x3);
const std::array arr2{ 1,2 };
//auto& [c_arr1, c_arr2] = arr2;
//c_arr1 = 10;//error
auto [f1, f2] = f();
print("{} {}\n", f1, f2);
std::map<int, std::string>Map{ {1,"*"},{2,"😘"} };
for (const auto& [m_a, m_b] : Map) {
print("{} {}\n", m_a, m_b);
}
}