我有一个指向向量的指针.现在,如何通过指针读取向量的内容?
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!)
}
这篇关于c++ - 如何从指向向量的指针访问向量的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!