指向矢量 [英] Pointer to a Vector

查看:114
本文介绍了指向矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个指向一个向量的指针。现在,如何通过指针读取向量的内容?我知道这是一个基本的问题,但我不能找到一个答案。(C ++)

I have a pointer to a vector. Now, how can I read the contents of the vector through pointer? I know this is a basic question, but Im unable to find an answer for the same.(C++)

推荐答案

解决方案,这里有一些我想出了:

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!)
}

这篇关于指向矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆