向量作为类成员

发布于 2024-12-22 01:03:42 字数 437 浏览 5 评论 0原文

你好我有这个问题: 我想要一个向量作为类成员。这也许是我的问题 对你来说更容易,对此我深表歉意。

  • 我应该如何声明向量?这是正确的吗? 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; or std::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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北座城市 2024-12-29 01:03:42

您肯定想使用 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.

糖粟与秋泊 2024-12-29 01:03:42

只需使用自动分配:将其声明为这样的成员:

class YourClass
{
    std::vector<int> myVector;
    // ...
};

该数组会在运行任何构造函数之前自动构造,并在对象被释放时自动销毁,您不需要关心它(而且,默认副本构造函数和赋值运算符将自动优雅地处理复制)。

相反,如果您只想在特定条件之后创建数组,则必须求助于(智能)指针和动态分配,但恕我直言,这非常麻烦(特别是因为您必须正确选择“三巨头” -复制构造函数、赋值运算符、析构函数);您可以简单地使用自动分配来分配向量,并使用单独的标志将数组标记为未初始化,或者只是检查其大小是否为 0。

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.

风筝在阴天搁浅。 2024-12-29 01:03:42

这完全取决于上下文 - 向量的含义以及您为什么需要它。它应该在多个对象之间共享吗?如果您不知道,请不要保留指针,请选择第二个选项。

std::vector<int> myVector;

如果您有充分的理由需要使用指针,那么请使用智能指针,它可以为您的情况提供最合适的所有权 - shared_ptrscoped_ptrunique_ptrwhatever_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.

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

半衾梦 2024-12-29 01:03:42

大多数时候,当我们使用标准库时,我们不需要关心内存分配/释放。模板会自动处理它。例如。 std::vector 的内存将根据该向量中存储的元素而增加或减少。这将是一个示例

因此,在您的情况下几乎可以这样使用它。

std::vector<int> myVector  //your second declaration
if(myCondition)
{
   myVector.push(some_int);  // use it directly
}

当您创建的 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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文