Reputation: 3433
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");
Upvotes: 0
Views: 1532
Reputation: 363817
You need
(*temp)[0].push_back("hello")
That's:
temp
to get a vector<vector<string> > &
vector<string> &
.
instead of ->
because you're no longer handling pointersThat said, it would be easier if someFunction
took a vector< 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 single vector
, not an optional one or an array of them).
Upvotes: 1