如何在c++中动态地将大小分配给数组?

时间:2022-10-07 13:16:47

So right now I have a class, called Set, in C++. In it, I have an integer called mCardinality, and an integer array called mElements. I want to be able to accept an integer in the constructor for mCardinality, then use this to assign a size to my integer array, is there any way to do this, or do I absolutely have to have a size for it right off the bat? (I guess I could just make the size the maximum integer, then only use the cardinality for limiting the loops where I deal with the elements, but a neater solution would be preferable).

现在我有一个类,叫集合,在c++中。在其中,我有一个名为mCardinality的整数,以及一个名为mElements的整数数组。我想要在mCardinality的构造函数中接受一个整数,然后用它为我的整数数组分配一个大小,有什么方法可以做到这一点吗,或者我必须马上为它设置一个大小?(我想我可以把大小设为最大整数,然后只使用基数来限制我处理元素的循环,但最好使用更整洁的解决方案)。

I did see on here that there may be a way to use std::vector to resize an array, will that work? And...how would that code look? And do vectors work just like arrays in C++ for everything else I want to do with it?

我在这里确实看到了使用std::vector来调整数组大小的方法,可以吗?和…代码是什么样子的?向量是否可以像c++中的数组一样工作?

4 个解决方案

#1


3  

Your options are these:

你的选择是:

  1. use std::vector or
  2. 或使用std::向量
  3. use plain old array
  4. 使用普通数组

With vector (recommended):

与向量(推荐):

class YourSet {
public:
   YourSet(int size) : elements(size)
   {
   }
private:
   std::vector<int> elements;
};

With array (not really recommended because of possible exception safety issues):

对于数组(由于可能的异常安全问题,并不推荐):

class YourSet {
public:
   YourSet(int size)
   {
       elements = new int[size];
   }
   ~YourSet()
   {
        delete[] elements;
   }
private:
   int* elements;
   // no implementation to prevent shallow copying
   YourSet( const YourSet&);
   void operator=(const YourSet&);
};

#2


1  

std::vector will work for your purposes.

向量将适用于您的目的。

#include <vector>

class Set
{
  Set(int len)
   : mElements(len)
  {
  }

  void do_smth()
  {
    size_t vector_len = mElements.size();

    int index = 0;
    int element = mElements[index];
  }


  std::vector<int> mElements;
};

#3


1  

Vector holds pointer, and when it`s needed reallocate memory.

Vector保存指针,当需要重新分配内存时。

T* new_ptr = new T[new_size];
std::copy(ptr, ptr + size, new_ptr);
delete[] ptr;
ptr = new_ptr;
size = new_size;

It is simple example. Vector uses allocator, but at fact something like this.

这是简单的例子。向量使用分配器,但实际上类似这样的东西。

#4


1  

GO for a vector. It will resize itself automatically when you add or remove items from it, and you can specify the size of it also if needed.

去一个向量。当您添加或删除项目时,它将自动调整大小,并且如果需要,您可以指定它的大小。

 vector<myClass> m_vClasses;
 myClass myNewObject;
 m_Classes.push_back(myNewObject); // Vector resized when you add new element

You won't need to worry about memory or other issues (unless myClass is a pointer that points to dynamically allocated memory) assuming that your myClass destructor correctly frees any memory that the class allocates. The vector will deal with the resizing for you automatically.

您不需要担心内存或其他问题(除非myClass是指向动态分配内存的指针),假设您的myClass销毁器正确地释放了类分配的内存。矢量将自动处理大小调整。

#1


3  

Your options are these:

你的选择是:

  1. use std::vector or
  2. 或使用std::向量
  3. use plain old array
  4. 使用普通数组

With vector (recommended):

与向量(推荐):

class YourSet {
public:
   YourSet(int size) : elements(size)
   {
   }
private:
   std::vector<int> elements;
};

With array (not really recommended because of possible exception safety issues):

对于数组(由于可能的异常安全问题,并不推荐):

class YourSet {
public:
   YourSet(int size)
   {
       elements = new int[size];
   }
   ~YourSet()
   {
        delete[] elements;
   }
private:
   int* elements;
   // no implementation to prevent shallow copying
   YourSet( const YourSet&);
   void operator=(const YourSet&);
};

#2


1  

std::vector will work for your purposes.

向量将适用于您的目的。

#include <vector>

class Set
{
  Set(int len)
   : mElements(len)
  {
  }

  void do_smth()
  {
    size_t vector_len = mElements.size();

    int index = 0;
    int element = mElements[index];
  }


  std::vector<int> mElements;
};

#3


1  

Vector holds pointer, and when it`s needed reallocate memory.

Vector保存指针,当需要重新分配内存时。

T* new_ptr = new T[new_size];
std::copy(ptr, ptr + size, new_ptr);
delete[] ptr;
ptr = new_ptr;
size = new_size;

It is simple example. Vector uses allocator, but at fact something like this.

这是简单的例子。向量使用分配器,但实际上类似这样的东西。

#4


1  

GO for a vector. It will resize itself automatically when you add or remove items from it, and you can specify the size of it also if needed.

去一个向量。当您添加或删除项目时,它将自动调整大小,并且如果需要,您可以指定它的大小。

 vector<myClass> m_vClasses;
 myClass myNewObject;
 m_Classes.push_back(myNewObject); // Vector resized when you add new element

You won't need to worry about memory or other issues (unless myClass is a pointer that points to dynamically allocated memory) assuming that your myClass destructor correctly frees any memory that the class allocates. The vector will deal with the resizing for you automatically.

您不需要担心内存或其他问题(除非myClass是指向动态分配内存的指针),假设您的myClass销毁器正确地释放了类分配的内存。矢量将自动处理大小调整。