I’m currently in the progress of teaching myself C++ and try to implement a simple hashmap (with two template classes ).
However, I can’t figure out how to initialize a dynamic array of vectors correctly.
My attempts which have failed:
std::vector<Key> *keys = new std::vector<Key>[size];
std::vector<Key> *keys = (std::vector<Key> *) malloc(sizeof(std::vector<Key>) * size);
std::vector<Key> **keys = reinterpret_cast<vector<Key> **>(malloc(sizeof(vector<Key>) * size));
Or was I doing somewhere else something wrong? 🙁
What you are doing is unnecessary, vector supports dynamic sizing and you do not need to
newit.So:
if you want to have a pointer to a vector of then you could new like so
Also you can always add and remove elements on demand and it will resize if necessary or you can force it: