向量< struct>的数组在C ++中 [英] array of vector<struct> in C++

查看:107
本文介绍了向量< struct>的数组在C ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构的矢量,它的组件,现在我想要这个组的数组,下面是我的代码

I've a vector of structure and it's components, now I want array of this group, below is my code

struct V1
{
USHORT val;
UINT cnt;
USHORT state;
};

struct V2
{
DWORD room;
vector <V1> vref;
bool update_V1(USHORT S1, USHORT S2);
VOID ClearState(USHORT S1);

};
struct V3
{
USHORT block;
vector <V2> V2ref;
bool Update_V2(DWORD S1,USHORT S2,USHORT S3);
VOID ClearState_V2(USHORT S4);
};
struct V4
{
USHORT space;
vector <V3> V3ref;
bool Update_V3(USHORT S1,DWORD S2,USHORT S3);
VOID ClearState_V2(USHORT S4);

};
struct V5
{
USHORT del_1;
vector <V4> V4ref;
bool Update_V4(USHORT S1,USHORT S2,DWORD S3,USHORT S4);
VOID ClearState_V2(USHORT S4);
};

class C1
{
vector<V5> V5ref[2];
bool UpdateGroup(USHORT S1,USHORT S2,USHORT S3,DWORD S4,USHORT S5);

}

bool C1::UpdateGroup(USHORT S1,USHORT S2,USHORT S3,DWORD S4,USHORT S5)
{
    vector<V5>::iterator it;
for ( it=V5ref[S5].begin() ; it< V5ref[S5].end(); it++ )
{
    if(it->del_1==S2)
    {
        return grpItr->Update_V4(S1,S2,S3,s4);
    }
}
V5 V5local;
V5local.del_1 = S2;
V5local.Update_V4(S1,S2,S3,S4);
V5ref[S5].push_back(V5local);
return true;
 }



我尝试使用向量V5ref [2];
它适用于第一次迭代,并抛出错误断言的第二次迭代,可能的原因。是有任何其他选项,有矢量的副本。

I tried using vector V5ref[2]; It works for 1st iteration and throws error "assert" for 2nd iteration, what could be the reason. is there any other option to have copies of vectors.

我想要做的是,参数S2是1,2,3,我想diff数组的S2 = 1,S2 = 2 ... V5的整个向量,它的组件应该是根据S2的数组的单独元素

what exactly I want to do is, with parameter S2 being 1, 2, 3, I want diff arrays of the whole vector for S2 = 1, S2 = 2...V5 and it's components should be seperate elements of the array according to S2

推荐答案

我>我研究了你的问题一点。由于代码不足,我们都只能猜测你在做什么或不在那里。如果向量没有足够的空间分配,调试断言通常会出现。 (纠正我,如果我错了)。因此,在这种情况下,在使用向量之前,您应该使用 resize(); 方法。这里是一个例子:

I have researched your problem a bit. Since the code is insufficient, we all can only guess what you are doing or not doing there. The debug assertion usually comes if the vector has not enough space allocated. (correct me if I am wrong). So, in this case, before using your vectors, you should use the resize(); method. Here is an example:

struct structure
{
    int value1;
    char value2;
    bool value3;
};

int _tmain(int argc, _TCHAR* argv[])
{
    std::vector<structure> vector1;
    vector1.resize(1);

    vector1[0].value1 = 12;
    vector1[0].value2 = 'h';
    vector1[0].value3 = true;

    return 0;
}

如果你自己测试,你会知道没有 vector.resize(1); 这将不会在运行时工作。

If you test it yourself, you will know that without the vector.resize(1); this won't work at the run-time.

这篇关于向量&lt; struct&gt;的数组在C ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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