简体   繁体   English

矢量作为班级成员

[英]Vector as a class member

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; or std::vector<int> myVector ? 或者std::vector<int> myVector
  • how should I handle this vector in dealloc? 我该如何在dealloc中处理这个向量?
  • How can I initialize the array into a if? 如何将数组初始化为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
}

You most certainly want to use std::vector<int> myVector . 你当然希望使用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. 无需初始化它,因为它会在类的构造函数中自动初始化,并在类被销毁时释放。

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。

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_ptrscoped_ptrunique_ptrwhatever_ptr

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. std :: vector的内存将根据存储在此向量中的元素增加或减少。 This would be an example . 这将是一个例子

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对象被销毁时,将使用释放的向量内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM