Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

How to push_back into empty 3d vector without segmentation fault

I need to push 2d vectors into a 3d vector based on a condition.

for()
 //check a condition on an element of 2d array. 
 //condition returns a variable i of value between 0 to 5.

 3dvector[i].push_back(2d[row]);

In this way, depending on the value of i, I write to different places in 3d vector. But this returns a segmentation fault because I'm accessing 3dvector[i] which during its first iteration for every new value of i, would be empty and hence throws the seg fault error.

What is the workaround?

Comments