Is an unordered_map really faster than a map in practice?

In response to questions about performance in relation to the number of missed searches, I have refactored the test to parameterise this. Example results: searches set_size miss ordered unordered flat_map 1000000 0 100% 4384 12901 681 1000000 99 99.99% 89127 42615 86091 1000000 172 99.98% 101283 53468 96008 1000000 303 99.97% 112747 53211 107343 1000000 … Read more

Using tuple in unordered_map

The template arguments for an unordered_map looks like this: template< class Key, class T, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key>, class Allocator = std::allocator< std::pair<const Key, T> > > class unordered_map; std::hash is not specialized for tuples (scroll down to Standard specializations for library types). Therefore you need to provide your own, something … Read more

Erasing elements from unordered_map in a loop

To comply with C++11 you’re unfortunately a bit limited in how you can tackle this. Your options basically boil down to: Iterate over the unordered_map and build a list of keys to delete like so: //std::unordered_map<…> mymap; std::vector<decltype(mymap)::key_type> vec; for (auto&& i : mymap) if (/*compare i*/) vec.emplace_back(i.first); for (auto&& key : vec) mymap.erase(key); Iterate … Read more

Does std::unordered_map operator[] do zero-initialization for non-exisiting key?

On the site you linked it says: When the default allocator is used, this results in the key being copy constructed from key and the mapped value being value-initialized. So the int is value-initialized: The effects of value initialization are: […] 4) otherwise, the object is zero-initialized This is why the result is 0.

Remove Elements from an Unordered Map Fulfilling a Predicate

The answer is no (you can’t use remove_if on associative containers). You need to do a simple loop; the erase(iterator) member now returns the next valid iterator – so your loop becomes: for(auto it = begin(m_map); it != end(m_map);) { if (it->second == 0) { it = m_map.erase(it); // previously this was something like m_map.erase(it++); … Read more

Is for(auto i : unordered_map) guaranteed to have the same order every time?

The iteration order of unordered associative containers can only change when rehashing as a result of a mutating operation (as described in C++11 23.2.5/8). You are not modifying the container between iterations, so the order will not change. Although the specification doesn’t explicitly state that rehashing can’t occur at any other time, doing so would … Read more

Does std::unordered_map equality depend on insertion order

Yes, they’re guaranteed to return equal in this case. The specific wording (from N4659, ยง[unord.req]/12) is: Two unordered containers a and b compare equal if a.size() == b.size() and, for every equivalent-key group [Ea1, Ea2) obtained from a.equal_range(Ea1), there exists an equivalent-key group [Eb1, Eb2) obtained from b.equal_range(Ea1), such that is_permutation(Ea1, Ea2, Eb1, Eb2) returns … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)