c++ - How to initialize unordered_map directly with fixed element? -


i want initialize 1 unordered_map fixed element 100. , keys 0 100, values of keys 0

using hashmap = unordered_map < int, int > ;  hashmap map; (int idx = 0; idx < 100; ++idx) {     map[idx] = 0; } 

question 1:

is there directly way following codes in python?

d = {x: x % 2 == 0 x in range(1, 11)} 

question 2:

with initialization codes above, think elements sorted in ascending order, results are:

enter image description here

why first element 8 , second element 64, left elements in ascending order?

  1. this not quite pretty python expression, should trick.

    #include <algorithm> #include <iostream> #include <iterator> #include <unordered_map>  int main() {     std::unordered_map<int, bool> m;     int = -1;     std::generate_n(std::inserter(m, m.begin()),                     10,                     [&i](){                              ++i;                              return std::make_pair(i, % 2 == 0);    });    (auto const &p: m)        std::cout << '<' << p.first << ", " << p.second << ">\n";    return 0; } 

    live on ideone.com

  2. there reason unordered maps called unordered maps. since implemented hash maps, keys not in predictable order. using std::unordered_map dense collection of integer keys not efficient solution problem, particularly if expect able extract keys in order.


Comments