#include <iostream>
#include <map>
//结构化绑定允许你用一个对象的元素或成员同事实例化多个实体.
struct stk_info {
float high;
float open;
float close;
float low;
int64_t t;
};
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
struct stk_info info{39.35, 39.05, 39.11, 39.05, 1719455713 };
// 把结构体中的成员绑定到新的变量中。
// 变量 h, o, c, l, t的声明方式称结构化绑定,其声明方式还可以表示为 auto [h, o, c, l, t]{info};
//h, o, c, l, t 是info中成员的拷贝,他们的生命周期相同,当结构化绑定离开作用域后info也会自动销毁。
// 除非使用引用(const auto& [...]),否则修改结构不会影响绑定
auto [h, o, c, l, t] = info;
// 使用结构化绑定遍历map
// 这么做的好处是可以直接访问成员,另外,把值绑定到能体现语义的变量名上,可以使代码的可读性更强。
std::map<int, std::string> map_;
map_[0] = "test1";
map_[1] = "test2";
map_[2] = "test3";
map_[3] = "test4";
for (const auto &[k, v]: map_) {
std::cout <<k << " : " << v << std::endl;
}
// 3、move
// 匿名实体是info右值的引用
auto&& [h1, o1, c1, l1,t1] = std::move(info);
//h1, o1, c1, l1,t1 指向的匿名实体是info的右值应用i,info的值任然保持不变。
std::cout << "info : " << info.high << ", "<< info.open << ", "<< info.close << ", "<< info.low << ", "<< info.t <<"\n";
std::cout << "[h1, o1, c1, l1,t1] "<< h1 << ", "<< o1 << c1 << ", "<< l1 << ", "<< t1 <<"\n";
// 如果将指向info.high的h1移走,在打印就是未定义了(不保证)
float hh = std::move(h1);
std::cout << "info.high : "<< info.high <<std::endl;
//4、类
// 不支持继承类中的结构化绑定
// 5、数组
int arr[] = {1,2};
auto [x, y] = arr; // 注意变量要匹配
return 0;
}