假设我有一个类型的对象
std::map<std::string, std::tuple<int, float>> data;
是否可以像这样以嵌套方式访问元素类型(即在范围 for 循环中使用时)
for (auto [str, [my_int, my_float]] : data) /* do something */
不,这是不可能的。
我清楚地记得在某处读到 C++17 不允许嵌套结构化绑定,但他们正在考虑在未来的标准中允许它。但找不到来源。
不,这是不可能的;但这是:
for (auto&& [key, value] : data) {
auto&& [my_int, my_float] = value;
}
至少很接近。
您可以在 C++20 中执行以下操作
for (auto& [my_int, my_float] : data | std::views::values)
{
// Some code
}