访问结构的矢量 [英] Accessing vectors of structs

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

问题描述

我有一个结构:

struct OutputStore 
{
    int myINT;
    string mySTRING;
}

如果我创建一个类型OutputStore的数组如下:

If I create an array of type OutputStore as follows:

OutputStore *OutputFileData = new OutputStore[100];

然后我可以解决这个问题:

then I can address it with:

OutputFileData[5].myINT = 27;

但是,如果我用一个矢量,而不是一个数组:

But if I use a vector instead of an array:

vector<OutputStore> *OutputFileData = new vector<OutputStore>(100);

然后我得到一个'......是不是会员的std ::矢量&lt; _Ty>的错误,如果我尝试:

Then I get an '... is not a member of 'std::vector<_Ty>' error if I try:

OutputFileData[5].myINT = 27;

因为你可以通过它的索引来访问矢量就像你可以在一个阵列,为什么这条线不能正常工作。
我只是想知道,因为它意味着我失踪的了解一些基本的一点。

Since you can access a vector via it's index just as you can an array, why does this line not work. I'm just interested to know as it suggests I'm missing some fundamental bit of understanding.

(我改为矢量因为我想的push_back,因为我不知道我的数据会达到的大小。我有它使用的构造结构,推回通过该工作...我只是想明白是怎么回事)

(I changed to a vector as I wanted to push_back as I do not know the size that my data will reach. I've got it to work by using a constructor for the structure and pushing back via that...I just want to understand what is going on here)

推荐答案

不要创建一个指向一个载体。只要做到

Don't create a pointer to a vector. Just do

vector<OutputStore> OutputFileData(100);

和你会没事的。为了使您的code以上工作,你需要做以下

And you'll be fine. To make your code above work you'll need to do the following

(*OutputFileData)[5].myINT = 27;

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

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