c++ - 如何从指向向量的指针访问向量的内容?

c++ - 如何从指向向量的指针访问向量的内容?

问题描述:

我有一个指向向量的指针.现在,如何通过指针读取向量的内容?

I have a pointer to a vector. Now, how can I read the contents of the vector through pointer?

解决方案有很多,以下是我想到的几个:

There are many solutions, here's a few I've come up with:

int main(int nArgs, char ** vArgs)
{
    vector<int> *v = new vector<int>(10);
    v->at(2); //Retrieve using pointer to member
    v->operator[](2); //Retrieve using pointer to operator member
    v->size(); //Retrieve size
    vector<int> &vr = *v; //Create a reference
    vr[2]; //Normal access through reference
    delete &vr; //Delete the reference. You could do the same with
                //a pointer (but not both!)
}