I have the following multi-dimensional vector
int main()
{
vector< vector<string> > tempVec;
someFunction(&tempVec);
}
void someFunction(vector< vector<string> > *temp)
{
//this does not work
temp[0]->push_back("hello");
}
How do i push data into the vector when i have a vector pointer?
The below code does not work.
temp[0]->push_back("hello");
You need
That’s:
tempto get avector<vector<string> > &vector<string> &.instead of->because you’re no longer handling pointersThat said, it would be easier if
someFunctiontook avector< vector<string> >&instead of a pointer:temp[0].push_back("hello"). References do not allow pointer arithmetic or null pointers, so they make it harder to screw up and are more suggestive of the actual kind of input required (a singlevector, not an optional one or an array of them).