在std :: map的范围内按结构化绑定声明中的值来获取内置类型

问题描述 投票:0回答:1

我有:

std::map<double, Foo> map;
for (auto&& [first, second] : map) { /* ... */ }

其中Foo是在其他地方声明的类

我目前看到两个问题:

1。映射类型常量[]

[second这里是Foo&,这是正确的,但是您也可以使用std :: as_const将其设置为const Foo&

for (auto&& [first, second] : std::as_const(map)) { /* ... */ }

所以这不是问题。

2。通过内置类型的值副本

[first在这里是const double&,对于内置类型而言效率低下。

是否有办法使first取值?

我确定这没有被忽略。

我有:std :: map 地图; for(auto && [first,second]:map){/ * ... * /}其中Foo是在其他地方声明的类我目前看来有2个问题:1.映射...

c++ dictionary for-loop associative-array structured-bindings
1个回答
0
投票

首先是const double&,对于内置类型而言效率低下。

© www.soinside.com 2019 - 2024. All rights reserved.