While creating a vector of defined size for further use in class, is it neccessary that the parameter for constructors of data members should be written in the initializer list of my class' constructor?
Why does
class MyHashMap {
public:
vector<int> vec;
MyHashMap():vec(1000001) {
fill(vec.begin(), vec.begin() + 1000000, -1);
}
works but,
class MyHashMap {
public:
vector<int> vec(1000001);
MyHashMap() {
fill(vec.begin(), vec.begin() + 1000000, -1);
}
does not work and gives an error like
Line 4: Char 22: error: expected parameter declarator
vector<int> vec (1000001);
^
If the same implementation is done using arrays,
class MyHashMap {
public:
int data[1000001];
MyHashMap() {
fill(data, data + 1000000, -1);
}
there is no issue.
I'm still a beginner at c++ if someone could help me that'd be awesome. Thanks