【问题标题】:What is the difference between these two ways of unordered_map declaration?这两种 unordered_map 声明方式有什么区别?
【发布时间】:2021-09-28 15:05:03
【问题描述】:

以下两种声明方式有什么区别?

unordered_map<int, int> mp;

auto umap = unordered_map<int, int>{};

和优化有关系吗?

【问题讨论】:

标签: c++ unordered-map


【解决方案1】:

对于unordered_map,从 c++17 开始就没有区别了。

如果类型是 e.g. 会有所不同聚合类型。

因此,如果您有 std::array,则会有不同的结果:

std::array<int, 4> a1;

auto a2 = std::array<int, 4>{};

a1 将包含不确定的值。

另一方面,它们是相等的:

std::array<int, 4> a1{};

auto a2 = std::array<int, 4>{};

所以使用auto var = some_type{};(也称为“几乎总是自动模式”)将确保您不会忘记初始化变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-07
    • 2012-04-01
    • 1970-01-01
    相关资源
    最近更新 更多