std::map

what is this in c++
std::map<std::string
It's an associative array, also known as dictionary.
See http://www.cplusplus.com/reference/stl/map/
why we are using map in c, what is the benefit behind this
It allows you to create structures that can very quickly be searched, using a non numerical key. I use it (or rather I use std::unordered_map) often.
What is advantage of std::unordered_map over std::map?
Quicker access?
map uses a red-black tree and is thus sorted. unordered_map, as the name suggests has no order; it uses a hash map, which is faster for lookups.
Thanks
One advantage with std::map is when you want to iterate over the elements in sorted by key order. Iterating over std::unordered_map doesn't guarantee any order.
Unordered map is much faster though...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
TR1 UNORDERED_MAP (4 byte objects, 10000000 iterations):
map_grow              126.1 ns  (27427396 hashes, 40000000 copies)  290.9 MB
map_predict/grow       67.4 ns  (10000000 hashes, 40000000 copies)  232.8 MB
map_replace            22.3 ns  (37427396 hashes, 40000000 copies)
map_fetch              16.3 ns  (37427396 hashes, 40000000 copies)
map_fetch_empty         9.8 ns  (10000000 hashes,        0 copies)
map_remove             49.1 ns  (37427396 hashes, 40000000 copies)
map_toggle             86.1 ns  (20000000 hashes, 40000000 copies)

STANDARD MAP (4 byte objects, 10000000 iterations):
map_grow              225.3 ns  (       0 hashes, 20000000 copies)  462.4 MB
map_predict/grow      225.1 ns  (       0 hashes, 20000000 copies)  462.6 MB
map_replace           151.2 ns  (       0 hashes, 20000000 copies)
map_fetch             156.0 ns  (       0 hashes, 20000000 copies)
map_fetch_empty         1.4 ns  (       0 hashes,        0 copies)
map_remove            141.0 ns  (       0 hashes, 20000000 copies)
map_toggle             67.3 ns  (       0 hashes, 20000000 copies)

Topic archived. No new replies allowed.