C++ 向量作为类成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8553464/
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
Vector as a class member
提问by Safari
Hello I have this question: I would like to have a vector as class member. This is perhaps my question easier for you and I apologize for that.
你好,我有这个问题:我想要一个向量作为班级成员。这可能是我的问题对你来说更容易,我为此道歉。
- how should I declare the vector? And is this correct?
std::vector<int> *myVector;
orstd::vector<int> myVector
? - how should I handle this vector in dealloc?
- How can I initialize the array into a if?
- 我应该如何声明向量?这是正确的吗?
std::vector<int> *myVector;
或者std::vector<int> myVector
? - 我应该如何在 dealloc 中处理这个向量?
- 如何将数组初始化为if?
Is this correct?
这样对吗?
if(myCondition)
{
if(!myVector) //is this correct?
myVector = new std::vector<int>(); //is this correct? on this i have a error
}
回答by fschoenm
You most certainly want to use std::vector<int> myVector
. No need to initialize it, as it gets automatically initialized in the constructor of your class and deallocated when your class is destroyed.
您肯定想使用std::vector<int> myVector
. 无需初始化它,因为它会在您的类的构造函数中自动初始化并在您的类被销毁时被释放。
回答by Matteo Italia
Just use automatic allocation: declare it as a member like this:
只需使用自动分配:将其声明为成员,如下所示:
class YourClass
{
std::vector<int> myVector;
// ...
};
The array gets constructed automatically before any of your constructor is run and is destroyed automatically when your object is deallocated, you don't need to care about it (also, the default copy constructor and assignment operator will handle copying gracefully automatically).
该数组在您的任何构造函数运行之前自动构造,并在您的对象被释放时自动销毁,您不需要关心它(此外,默认的复制构造函数和赋值运算符将自动优雅地处理复制)。
If, instead, you want to create the array only after a particular condition, you have to resort to a (smart) pointer and dynamic allocation, but IMHO it's quite cumbersome (especially because you then have to get right the "big three" - copy constructor, assignment operator, destructor); you could instead simply allocate the vector with automatic allocation and use a separate flag to mark your array as not initialized, or just check if its size is 0.
相反,如果您只想在特定条件之后创建数组,则必须求助于(智能)指针和动态分配,但恕我直言,这非常麻烦(尤其是因为您必须正确处理“三巨头”-复制构造函数、赋值运算符、析构函数);您可以简单地使用自动分配来分配向量,并使用单独的标志将您的数组标记为未初始化,或者只是检查其大小是否为 0。
回答by Armen Tsirunyan
That depends entirely on context - what the vector means and why you need it. Should it be shared among multiple objects? If you don't know, don't keep a pointer, go with your second option.
这完全取决于上下文 - 向量意味着什么以及为什么需要它。它应该在多个对象之间共享吗?如果您不知道,请不要保留指针,请选择第二个选项。
std::vector<int> myVector;
If you have strong reasons to have a pointer, then please use a smart pointer, the one that provides most appropriate ownership for your situation - shared_ptr
, scoped_ptr
, unique_ptr
or whatever_ptr
如果你有充分的理由有一个指针,那么请使用智能指针,提供最适合的所有权您的具体情况之一- ,,或shared_ptr
scoped_ptr
unique_ptr
whatever_ptr
回答by tyger
Most of the time, when we use standard library, We do not need to care about the memory allocation/deallocation. The template will handle it automatically. eg. The memory of a std::vector will be increase or decrease according to the elements stored in this vector. This would be an example.
大多数时候,当我们使用标准库时,我们不需要关心内存分配/释放。模板将自动处理它。例如。std::vector 的内存将根据存储在该向量中的元素增加或减少。这将是一个例子。
Therefore, almost you can use it this way in your case.
因此,几乎您可以在您的情况下以这种方式使用它。
std::vector<int> myVector //your second declaration
if(myCondition)
{
myVector.push(some_int); // use it directly
}
The memory the vector used will be deallocated when the Class object you created is destroyed.
当您创建的 Class 对象被销毁时,vector 使用的内存将被释放。