如何在C ++中从指向向量的指针访问向量的内容? [英] How to access the contents of a vector from a pointer to the vector in C++?

查看:110
本文介绍了如何在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!)
}

这篇关于如何在C ++中从指向向量的指针访问向量的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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