向量作为类成员
你好我有这个问题: 我想要一个向量作为类成员。这也许是我的问题 对你来说更容易,对此我深表歉意。
- 我应该如何声明向量?这是正确的吗?
std::vector
或; *myVector; std::vector
; myVector ? - 我应该如何在 dealloc 中处理这个向量?
- 如何将数组初始化为 if ?
这是正确的吗?
if(myCondition)
{
if(!myVector) //is this correct?
myVector = new std::vector<int>(); //is this correct? on this i have a error
}
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?
Is this correct?
if(myCondition)
{
if(!myVector) //is this correct?
myVector = new std::vector<int>(); //is this correct? on this i have a error
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您肯定想使用 std::vector; myVector 。不需要初始化它,因为它会在类的构造函数中自动初始化,并在类被销毁时释放。
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.只需使用自动分配:将其声明为这样的成员:
该数组会在运行任何构造函数之前自动构造,并在对象被释放时自动销毁,您不需要关心它(而且,默认副本构造函数和赋值运算符将自动优雅地处理复制)。
相反,如果您只想在特定条件之后创建数组,则必须求助于(智能)指针和动态分配,但恕我直言,这非常麻烦(特别是因为您必须正确选择“三巨头” -复制构造函数、赋值运算符、析构函数);您可以简单地使用自动分配来分配向量,并使用单独的标志将数组标记为未初始化,或者只是检查其大小是否为 0。
Just use automatic allocation: declare it as a member like this:
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.
这完全取决于上下文 - 向量的含义以及您为什么需要它。它应该在多个对象之间共享吗?如果您不知道,请不要保留指针,请选择第二个选项。
如果您有充分的理由需要使用指针,那么请使用智能指针,它可以为您的情况提供最合适的所有权 -
shared_ptr
、scoped_ptr
、unique_ptr
或whatever_ptr
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.
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
orwhatever_ptr
大多数时候,当我们使用标准库时,我们不需要关心内存分配/释放。模板会自动处理它。例如。 std::vector 的内存将根据该向量中存储的元素而增加或减少。这将是一个示例。
因此,在您的情况下几乎可以这样使用它。
当您创建的 Class 对象被销毁时,向量使用的内存将被释放。
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.
Therefore, almost you can use it this way in your case.
The memory the vector used will be deallocated when the Class object you created is destroyed.