-2

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;
 }

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.

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

user1586695
  • 39
  • 1
  • 9
  • 1
    We can't really know what you are doing there... you said `I tried using vector V5ref[2]` but there's not `V5ref` vector. Also you should write more of your code, not just the header file. – Victor Jan 24 '14 at 06:57
  • 2
    Note *more* code, but *relevant* code. – juanchopanza Jan 24 '14 at 07:02

2 Answers2

2

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;
}

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

Victor
  • 13,914
  • 19
  • 78
  • 147
  • I think this might be a poor introduction to vector usage and tends to suggest you might be better suited to using std::array – kfsone Jan 24 '14 at 07:26
  • It really isn't the perfect code, sinnce the OP should initialize the somehow, but I need some more code to figure out what he wants. – Victor Jan 24 '14 at 07:29
0

At any given time, a std::vector<> has a constraint, size which is the maximum element number you can access + 1, and a constraint capacity which is how many elements it can contain before it has to move the data to a larger memory allocation.

size determines what you can access. When the vector is created, you cannot access anything. vec[0] is illegal when the vector is empty.

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> vec;

    std::cout << "initially, vec.size = " << vec.size() << ", cap = " << vec.capacity() << "\n";

    // initially, the vector is empty. 

    // std::cout << vec[0] << '\n'; // undefined behavior, probably a crash
    // you can only ever access vec[n] where v < vec.size(),
    // this vector is empty, so size() == 0

    // Lets add a number to the vector.
    vec.push_back(5);
    std::cout << "now, vec.size = " << vec.size() << ", cap = " << vec.capacity()         

    std::cout << "vec[0] = " << vec[0] << '\n';
    // std::cout << vec[1] << '\n'; // undefined behavior because 1 >= size()

    vec.resize(5);
    std::cout << "resized, vec.size = " << vec.size() << ", cap = " << vec.capacity()         

    vec[1] = 1; // valid, resize made space for vec[0] .. vec[4]
    vec[2] = 2;

    for (auto it = vec.begin(), end = vec.end(); it != end; ++it)
        std::cout << *it << '\n';

    return 0;
}

push_back does "vec.resize(vec.size() + 1)" for you, and then inserts the value being 'push_back'ed into the new slot.

resize attempts to make room for extra elements -- if size is 3 and you say resize(5) it will try to make room for 2 new elements.

If the result causes size to exceed capacity then the vector allocates a new array, copies the old data over into it, and releases the old array. This can become very expensive. If you know roughly how big your vector is going to become, you can avoid this relocation by calling reserve()

#include <iostream>
#include <vector>

using std::cout;

struct Stud // will always tell you when it reproduces
{
    int m_i;
    Stud() : m_i(-1) {}
    Stud(int i) : m_i(i) {}
    Stud(const Stud& rhs) : m_i(rhs.m_i) { cout << "Copying(Ctor) " << m_i << '\n'; }
    Stud& operator=(const Stud& rhs) { m_i = rhs.m_i; cout << "Copying(=) " << m_i << '\n'; return *this; }
};

int main()
{
    std::vector<Stud> studs;

    studs.push_back(0);
    studs.push_back(1);

    studs.reserve(5);

    studs.push_back(5); // remember, this adds to the back.
    studs.push_back(6);

    std::cout << "size of studs " << studs.size();

    return 0;
}

Based on this

bool Update_V4(USHORT S1,USHORT S2,DWORD S3,USHORT S4);

I am guessing that you are trying to simulate multi-dimensional arrays of some kind, and you are doing something like

V4Ref[S1]

without checking

if (S1 < V4Ref.size())

C++ has a function 'assert' which will cause a Debug build application to terminate if a condition is not met.

#include <cassert>
#include <vector>

int main()
{
    std::vector<int> vec;
    assert(vec.size() == 0);
    vec.push_back(1);
    vec.resize(5);
    vec.push_back(5);
    assert(vec.size() == 10); // expect to crash here.

    return 0;
}

You could use this to help catch bad sizes:

bool V4::Update_V4(USHORT S1,USHORT S2,DWORD S3,USHORT S4)
{
    assert(S1 < V4ref.size());
    return V4ref[S1].Update_V3(S2, S3, S4);
}
kfsone
  • 23,617
  • 2
  • 42
  • 74