对于map,tuple等类型,C++17可以通过结构化的绑定到对应的数据结构上:
#include #include using namespace std; int main() { map m = { {1, "hi"}, {2, "hello"}}; for(auto [index, str] : m) //将index和str直接绑定到map的内部对象上 { cout<<"index:"< 这种结构化绑定也可以将变量绑定为引用及const类型: #include #include using namespace std; int main() { map m = { {1, "hi"}, {2, "hello"}}; for(auto &[index, str] : m) { str+=" !"; } for(const auto &[index, str] : m) { cout<<"index:"< 说起来,结构化绑定其实有些像TypeScript里的解构,也可以用于绑定数组和对象里的: #include using namespace std; class A{ public: int d1 = 1; int d2 = 2; }; int main() { int a[] = {1, 2, 3}; auto &[m1,m2,m3] = a; //绑定到数组 cout<
这种结构化绑定也可以将变量绑定为引用及const类型:
#include #include using namespace std; int main() { map m = { {1, "hi"}, {2, "hello"}}; for(auto &[index, str] : m) { str+=" !"; } for(const auto &[index, str] : m) { cout<<"index:"< 说起来,结构化绑定其实有些像TypeScript里的解构,也可以用于绑定数组和对象里的: #include using namespace std; class A{ public: int d1 = 1; int d2 = 2; }; int main() { int a[] = {1, 2, 3}; auto &[m1,m2,m3] = a; //绑定到数组 cout<
说起来,结构化绑定其实有些像TypeScript里的解构,也可以用于绑定数组和对象里的:
#include using namespace std; class A{ public: int d1 = 1; int d2 = 2; }; int main() { int a[] = {1, 2, 3}; auto &[m1,m2,m3] = a; //绑定到数组 cout<
上一篇 文件的读写,操作模式
下一篇 L1D1:嵌入式Linux C语言开发工具及基础命令
版权所有 (c)2021-2022 MSHXW.COM
ICP备案号:晋ICP备2021003244-6号