结构中的向量 - 最佳方法?C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6246793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:45:47  来源:igfitidea点击:

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

c++vectorstruct

提问by Satchmo Brown

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.

编辑:问题在于它最终导致指向 xmemory.h 的错误,该文件包含在 MSVC++ 平台中。

    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?

有趣的是,如果我将它分配在结构之外并且只是在我使用的函数中分配,则不会发生这种情况。有任何想法吗?

回答by StackedCrooked

You can write it like this without the typedef:

你可以在没有 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 operatorand 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.

编辑:修复赋值运算符:检查与 this 的比较并返回 *this。

回答by Puppy

It makes complete sense to do that and you don't need newin any respect, unless you actually want to alias a separate vector. In addition, you don't need any typedefstuff going on here.

这样做是完全有意义的,并且您new在任何方面都不需要,除非您确实想为单独的向量设置别名。此外,您不需要typedef在这里进行任何操作。

回答by Merlyn Morgan-Graham

It depends on how you use it.

这取决于你如何使用它。

If you want to copy the vector and data when copying the Regionstruct, then leave it as a non-pointer.

如果要在复制Region结构体时复制向量和数据,则将其保留为非指针。

If you don't want it copied over, then you will want some sort of pointer to a vector.

如果您不想复制它,那么您将需要某种指向向量的指针。

If you use a pointer to a vector, you should be very careful about allocation/deallocation exception safety. If you can't scope your allocation in an exception safe way, then you'll leave a potential for memory leaks.

如果您使用指向向量的指针,您应该非常小心分配/解除分配异常安全。如果您不能以异常安全的方式确定您的分配范围,那么您就有可能发生内存泄漏。

A couple options are:

几个选项是:

  • Make sure that the code that allocates the vector (and uses the Region) also deallocates the vector, and is itself exception safe. This would require the Regionto only exist inside that code's scope.
    You could do this by simply allocating the vector on the stack, and pass that to the pointer in the Region. Then make sure you never return a Regionobject above that stack frame.
  • You could also use some sort of smart pointer -> vector in your Region.
  • 确保分配向量(和使用Region)的代码也释放向量,并且本身是异常安全的。这将要求Region仅存在于该代码的范围内。
    您可以通过简单地在堆栈上分配向量,并将其传递给Region. 然后确保您永远不会返回Region该堆栈帧上方的对象。
  • 您还可以在Region.

回答by Steve Jessop

The vector is fine. Be aware that if you copy this struct, then the vector will be copied with it. So in code with particular performance constraints, treat this struct the same way that you'd treat any other expensive-to-copy type.

向量没问题。请注意,如果您复制此结构,则向量将与它一起复制。因此,在具有特定性能约束的代码中,请像对待任何其他复制成本高的类型一样对待此结构。

In production code, some people would prefer you to use the classkeyword rather than the structkeyword to define this class, since the vectormember makes it non-POD. If you're the author of your own style guide there's nothing to worry about.

在生产代码中,有些人更喜欢您使用class关键字而不是struct关键字来定义此类,因为该vector成员使其成为非 POD。如果您是自己风格指南的作者,则无需担心。

The typedefis wrong, though, just write struct Region { stuff };

typedef是错的,但是,只写struct Region { stuff };