Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A vector in a struct - best approach? C++

Tags:

c++

struct

vector

I am trying to include a vector in my struct.

Here is my struct:

struct Region 
{
    bool hasPoly;
    long size1; 
    long size2;
    long size3;
    long size4;
    long size5;
    long size6;
    //Mesh* meshRef; // the mesh with the polygons for this region
    long meshRef;
    std::vector<int> PVS;
} typedef Region;

Is the vector in this declaration valid or would it make more sense to do a pointer to a vector. In the case of a pointer to a vector, do I need to allocate a new vector. How would I accomplish this?

Thanks!

Edit: The problem is that it ends up causing an error that points to xmemory.h, a file included with the MSVC++ platform.

    void construct(pointer _Ptr, _Ty&& _Val)
        {   // construct object at _Ptr with value _Val
        ::new ((void _FARQ *)_Ptr) _Ty(_STD forward<_Ty>(_Val)); // this is the line
         }

Interestingly, it does not happen if I allocate it outside of the struct and simply in the function I use. Any ideas?

like image 998
Satchmo Brown Avatar asked Jun 06 '11 00:06

Satchmo Brown


People also ask

Can I have a vector in a struct?

You can actually create a vector of structs!

Can I store a vector in a struct C++?

Storing a vector inside the struct doesn't make it change its size. Vector's items are stored on the heap, so the size of the struct remains the same, no matter if the vector has 0 or 1000 elements.

Is vector allowed in C?

Vectors are a modern programming concept, which, unfortunately, aren't built into the standard C library. Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container.

How can we use vector in C?

In this tutorial, we will be discussing a program to understand how vectors work in C/C++. A vector data structure is an enhancement over the standard arrays. Unlike arrays, which have their size fixed when they are defined; vectors can be resized easily according to the requirement of the user.


1 Answers

You can write it like this without the typedef:

struct Region 
{
    bool hasPoly;
    long size1; 
    long size2;
    long size3;
    long size4;
    long size5;
    long size6;
    long meshRef;
    std::vector<int> PVS;
}; // no typedef required

To answer your questions:

Is the vector in this declaration valid

Yes, it is.

or would it make more sense to do a pointer to a vector.

No, probably not. If you did then you would have to implement copy constructor, assignment operator and destructor for the copy behavior. You would end up with the same but it would be extra work and potentially introduce bugs.

In the case of a pointer to a vector, do I need to allocate a new vector. How would I accomplish this?

You would need to implement the copy constructor, the copy assignment operator and the destructor:

// Copy constructor
Region(const Region & rhs) :
    hasPoly(rhs.hasPoly),
    // ... copy other members just like hasPoly above, except for PVS below:
    PVS(new std::vector<int>(*rhs.PVS))
{
}

// Copy assignment operator
Region & operator=(const Region & rhs)
{
    if (this != &rhs)
    {
         hasPoly = rhs.hasPoly;
         // ... copy all fields like hasPoly above, except for PVS below:

         delete PVS;
        PVS = new std::vector<int>(*rhs.PVS);
    }
    return *this;
}

// Destructor
Region::~Region()
{
    delete PVS;
}

Bottom line: your code is fine. You don't need to change it.

EDIT: Fix assignment operator: check for comparison against this and return *this.

like image 140
StackedCrooked Avatar answered Sep 23 '22 08:09

StackedCrooked