【发布时间】:2020-01-12 15:09:16
【问题描述】:
我遇到了以下代码:
vector<pair<int, int>> vec;
//...
for (auto &[f, s] : vec)
{
//do something with f and s
}
这种语法是如何工作的([f, s] : vec)以及它引入了什么标准? 我可以使用它从任何结构/类中获取字段值还是它是特定于元组/对的东西?
另外,这种方法对性能有何影响?
在 C++11 中,我使用 auto 的方式如下:
for (auto &it : vec)
{
//do something with it.first and it.second
}
【问题讨论】:
-
这能回答你的问题吗? What are use cases for structured bindings?
-
性能影响?不明显(如果有的话)。
标签: c++