I need to create a struct that sotres a std::vector<std::vector<int>>
that will contain 4 vectors of ints. I tried the declaration I usually use for vector of vectors:
struct myStruct{
std::vector<std::vector<int>> myVector(4);
};
but, when I compile, I get this error:
myProgram.cpp:79:52: error: expected identifier before numeric constant
std::vector<std::vector<int>> myVector(4);
^
myProgram.cpp:79:52: error: expected ‘,’ or ‘...’ before numeric constant
I tried to declare the vector in the struct and then reserve 4 elements in the main(), in the following way:
struct myStruct{
std::vector<std::vector<int>> myVector;
};
int main(){
myStruct s;
s.myVector.reserve(4);
s.myVector[0].push_back(1);
return 0;
}
In this way it compiles without errors, but I get a segmentation violation as soon as I try to push_back.
What is the proper way to do this task? And why can't I use the first declaration to specify the size of myVector? Thank you! 🙂
Please login or Register to submit your answer