C++17尝鲜:结构化绑定声明(Structured Binding Declaration)
来源:互联网 发布:红色痘印怎么消除 知乎 编辑:程序博客网 时间:2024/06/02 00:30
结构化绑定声明
结构化绑定声明,是指在一次声明中同时引入多个变量,同时绑定初始化表达式的各个子对象的语法形式。
结构化绑定声明使用auto来声明多个变量,所有变量都必须用中括号括起来。
cv-auto+引用 [变量1, 变量2, ... 变量n ] = 初始化表达式;cv-auto+引用 [变量1, 变量2, ... 变量n ] (初始化表达式);cv-auto+引用 [变量1, 变量2, ... 变量n ] {初始化表达式};// 这里 cv-auto+引用 包含 auto, auto const, auto &, auto&& 等等形式
结构化绑定所声明的变量有两种形式:
1. 非引用变量,此时初始化表达式对象需要拷贝一份,变量所绑定的是初始化表达式对象拷贝的各个子对象。
2. 引用变量,此时初始化表达式对象不需要拷贝,变量所绑定的是初始化表达式对象本身的各个子对象。
结构化绑定中的初始化表达式有三种类型:
1. 数组类型,此时变量所绑定的是数组的各个元素。
2. pair
tuple
等支持 tuple_size<E>
的类型,此时变量所绑定的是 get<0>(e),get<1>(e),get<2>(e)
…
这里E是指类型,e是指对象。
3. 带有 public 成员的结构类型,此时变量所绑定的是结构对象的各个 public 成员。
C++17代码
#include <iostream>#include <utility>#include <set>#include <map>using namespace std;struct S { int a, b;};map<string, int> get_map(){ return { { "hello", 1 }, { "world", 2 }, { "it's", 3 }, { "me", 4 }, };}int main(){ auto [a, b] = pair(2, "3"s); cout << a << b << endl; // 23 set<string> myset; if (auto [iter, success] = myset.insert("Hello"); success) cout << *iter << endl; // Hello int arr[] = {1, 2}; const auto& [m, n] = arr; cout << m << n << endl; //12 S s = {4, 5}; auto& [x, y] = s; x = 0, y = 1; cout << s.a << s.b << endl; // 01 for (auto&& [k, v] : get_map()) cout << "k=" << k << " v=" << v << endl; // k=hello v=1 // k=it's v=3 // k=me v=4 // k=world v=2}
代码说明
- 代码第25行采用结构化绑定声明了 int 类型的 a 和 string 类型的 b,分别绑定了初始化表达式中 pair 对象的 first 和 second。
- 代码第29行采用结构化绑定声明了迭代类型的 iter 和 bool 类型的 success,分别绑定了初始化表达式中 pair 对象的 first 和 second。
- 代码第33行采用结构化绑定声明了 const int& 类型的 m 和 n,分别绑定了初始化表达式中 arr 数组的两个元素。
- 代码第37行采用结构化绑定声明了 int& 类型的 x 和 y,分别绑定了初始化表达式中 s 对象的两个数据成员 a 和 b。
- 代码第41行采用结构化绑定声明了 string&& 类型的 k 和 int 类型的 v,分别绑定了初始化表达式中 pair 对象的 first 和 second。
C++14等价代码
#include <iostream>#include <utility>#include <set>#include <map>using namespace std;struct S { int a, b;};map<string, int> get_map(){ return { { "hello", 1 }, { "world", 2 }, { "it's", 3 }, { "me", 4 }, };}int main(){ auto kv = make_pair(2, "3"s); auto& a = kv.first; auto& b = kv.second; set<string> myset; set<string>::iterator iter; bool success; tie(iter, success) = myset.insert("Hello"); if (success) cout << *iter << endl; // Hello int arr[] = {1, 2}; const auto &m = arr[0], &n = arr[1]; cout << m << n << endl; //12 S s = {4, 5}; auto &x = s.a, &y = s.b; x = 0, y = 1; cout << s.a << s.b << endl; // 01 for (auto&& kv : get_map()) { auto&& k = forward<decltype(kv.first)>(kv.first); auto&& v = forward<decltype(kv.second)>(kv.second); cout << "k=" << k << " v=" << v << endl; } // k=hello v=1 // k=it's v=3 // k=me v=4 // k=world v=2}
阅读全文
1 0
- C++17尝鲜:结构化绑定声明(Structured Binding Declaration)
- 结构化数据(structured),半结构化数据(semi-structured),非结构化数据(unstructured)
- c#--wpf数据绑定binding
- WCF 绑定(Binding)
- 数据绑定(Binding)
- 结构化预测(structured prediction)
- 结构化思维(Structured Thinking)
- 结构化数据(structured),半结构化数据(semi-structured),非结构化数据(unstructured)的定义和区别
- 前置声明(Forward declaration)
- 前置声明(Forward declaration)
- 配置声明(Configuration Declaration)
- (翻译)结构化数据(Structured Format)
- Oracle结构化查询语言(Structured Query Language)
- 静态绑定(static-binding) 动态绑定(dynamic-binding)
- 关于“绑定(binding)”技术
- 数据绑定(Data Binding)
- 数据绑定(data binding)
- 什么是结构化方法 Structured Method
- 【日期类】C++实现
- git 遇到的问题
- VirtualAPK框架接入
- Kotlin之Lambda表达式原理及应用
- MySQL 自动编号与主键约束
- C++17尝鲜:结构化绑定声明(Structured Binding Declaration)
- java的Object类
- LeetCode 454. 4Sum II (Medium)
- 最透彻的关于“随机数种子”和“伪随机数”的产生原理
- Spring Boot与Log4j2集成之java.lang.IllegalStateException: Logback configuration error detected:
- 过滤器和拦截器的区别
- [Scala--基础]---Scala枚举使用
- MySQL 初涉唯一约束
- 通过HttpClient来调用Web Api接口