[英]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()
. 我有一个
Thing
类,带有一个Thing::Thing()
构造函数和一个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. 我正在尝试创建
arrayOfVectors
,以使数组内的每个std :: vector的大小均为0。构造函数正确地打印出每个矢量的大小,但print()
方法没有。
I have tried calling arrayOfVectors[n].clear()
and arrayOfVectors[n].assign(0,0)
on each vector within the array but did not work. 我试过在数组中的每个向量上调用
arrayOfVectors[n].clear()
和arrayOfVectors[n].assign(0,0)
,但是没有用。
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
. 它是
Thing
类的成员变量。 It is a completely separate variable from the local store
variable. 它是与本地
store
变量完全独立的变量。
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. 您的构造函数永远不会初始化成员变量,因此,当您间接调用
print
成员函数中的指针时,程序的行为是不确定的。
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?
如何避免在标头定义中设置特定数字(例如5)?
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
: 创建动态数组的惯用方式是使用
std::vector
:
class Thing {
private:
std::vector<std::vector<int>> vectorOfVectors{5};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.