简体   繁体   中英

What is the difference between these two ways of unordered_map declaration?

What is the difference between below two ways of declarations?

unordered_map<int, int> mp;

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

Is there something to do with optimization?

For an unordered_map it won't make a difference since c++17.

It will make a difference if the type is eg an aggregate type.

So if you have and std::array , there would be a different outcome for:

std::array<int, 4> a1;

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

a1 will contain indeterminate values.

On the other hand, those would be equal:

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

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

So using auto var = some_type{}; (which is also referred to as "almost always auto pattern") would ensure that you don't forget to initialize a variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM