简体   繁体   中英

Initialize array of vectors where each vector is size 0

I have a class Thing with a constructor Thing::Thing() and a method Thing::print() . I am trying to create arrayOfVectors such that each std::vector within the array is of size 0. The constructor function prints out the sizes of each vector correctly but the print() method does not.

I have tried calling arrayOfVectors[n].clear() and arrayOfVectors[n].assign(0,0) on each vector within the array but did not work.

Thing.hpp

class Thing {
private:
  std::vector<int>* arrayOfVectors;

public:
  Thing();
  void print() const;
};

Thing.cpp

Thing::Thing() {
  std::vector<int> arrayOfVectors[5];
  std::cout << arrayOfVectors[0].size() << std::endl; // 0
  std::cout << arrayOfVectors[1].size() << std::endl; // 0
  std::cout << arrayOfVectors[2].size() << std::endl; // 0
  std::cout << arrayOfVectors[3].size() << std::endl; // 0
  std::cout << arrayOfVectors[4].size() << std::endl; // 0
}

void Thing::print() const {
  std::cout << arrayOfVectors[0].size() << std::endl; // 0
  std::cout << arrayOfVectors[1].size() << std::endl; // 35183230189065
  std::cout << arrayOfVectors[2].size() << std::endl; // 33
  std::cout << arrayOfVectors[3].size() << std::endl; // 35
  std::cout << arrayOfVectors[4].size() << std::endl; // 108
}

main.cpp

int main() {
  Thing thing;
  thing.print();
  return 0;
}
 Thing::Thing() { std::vector<int> store[5]; ^^^^^^^^^^^^^^^^^^^^^^^^^ 

This here is an array of vectors. It is an automatic variable. Automatic variables are destroyed at the end of the block where they were created. In this case, the local array is destroyed at the end of the constructor call.

 class Thing { private: std::vector<int>* arrayOfVectors; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

This here is a pointer to a vector. It is not an array. It is a member variable of the class Thing . It is a completely separate variable from the local store variable.

Your constructor never initializes the member variable, so when you indirect the pointer in the print member function, the behaviour of the program is undefined.


If you want your class to have an array as a member variable, you can write it like this:

class Thing {
private:
  std::vector<int> arrayOfVectors[5];

You don't need to declare a constructor, since the automatically generated one does exactly what you want - all vectors will be empty.


How can i avoid setting a specific number like 5 in the header definition?

You cannot avoid that with an array variable. The size must be known at the time of compilation. If you need an array with non-fixed size, you need to allocate the array in the dynamic storage. The idiomatic way to create a dynamic array is to use std::vector :

class Thing {
private:
  std::vector<std::vector<int>> vectorOfVectors{5};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM