I'm trying to find a solution to what may be a very trivial problem. I would like to initialize my const unordered_map
in the class initializer list. However I'm yet to find the syntax that the compiler (GCC 6.2.0) will accept. A code link is here.
#include <unordered_map>
class test {
public:
test()
: map_({23, 1345}, {43, -8745}) {}
private:
const std::unordered_map<long, long> map_;
};
Error:
main.cpp: In constructor 'test::test()':
main.cpp:6:36: error: no matching function for call to 'std::unordered_map<long int, long int>::unordered_map(<brace-enclosed initializer list>, <brace-enclosed initializer list>)'
: map_({23, 1345}, {43, -8745}) {}
^
Are the complex constants not allowed to be initialized in the initializer list? Or the syntax has to be different?
The value object is value-initialized, not zero-initialized.
Internally unordered_map is implemented using Hash Table, the key provided to map are hashed into indices of a hash table that is why the performance of data structure depends on hash function a lot but on an average, the cost of search, insert and delete from the hash table is O(1).
unordered_map::clear() function is used to remove all elements from the container. When this function is applied to unordered_map its size becomes zero. Return type: This function return nothing.
Use braces instead of the parentheses
class test {
public:
test()
: map_{{23, 1345}, {43, -8745}} {}
private:
const std::unordered_map<long, long> map_;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With