-
Vector/Pointer Help
Hi,
If i have a pointer to a vector what is the correct syntax to access an element of the vector?
For example:
Code:
void myfunc(std::vector<std::string> *p)
{
...
my_str = p[i]->mystring;
...
}
As expected this obviously fails, but whats the correct syntax for accessing an element at a position inside a vector?
Thanks for any help,
Jack
-
foo->bar is just shorthand for (*foo).bar. If p was a vector, you would either use p[i] or p.at(i) to reference. Since p is now a pointer, *p is a vector, so (*p)[i] or (*p).at(i). This last you could write as p->at(i).
-