C++ 如何创建指针数组?

C++ 如何创建指针数组?,c++,arrays,pointers,C++,Arrays,Pointers,我正在尝试创建一个指针数组。这些指针将指向我创建的学生对象。我该怎么做? 我现在得到的是: Student * db = new Student[5]; 但该数组中的每个元素都是学生对象,而不是指向学生对象的指针。 谢谢。\include std::向量db(5); //使用中 db[2]=&someStudent; 这样做的好处是,您不必担心删除分配的存储—vector会为您完成这项工作 指针数组被写为指针的指针: Student **db = new Student*[5]; 现在的问

我正在尝试创建一个指针数组。这些指针将指向我创建的学生对象。我该怎么做? 我现在得到的是:

Student * db = new Student[5];
但该数组中的每个元素都是学生对象,而不是指向学生对象的指针。 谢谢。

\include
std::向量db(5);
//使用中
db[2]=&someStudent;

这样做的好处是,您不必担心删除分配的存储—vector会为您完成这项工作

指针数组被写为指针的指针:

Student **db = new Student*[5];
现在的问题是,您只为五个指针保留了内存。因此,您必须遍历它们来创建Student对象本身

在C++中,对于大多数用例来说,STD:生命是更容易的::vector。
std::vector<Student*> db;
std::vector db;
现在您可以使用push_back()向其添加新指针,并使用[]对其进行索引。它比**东西更干净。

void main()
    void main()
    {
    int *arr;
    int size;
    cout<<"Enter the size of the integer array:";
    cin>>size;
    cout<<"Creating an array of size<<size<<"\n";
        arr=new int[size];
    cout<<"Dynamic allocation of memory for memory for array arr is successful";
    delete arr;
    getch();enter code here
}
{ int*arr; 整数大小; coutsize;
coutWhile vector是一种建议的方法,它可以包含一个内容列表,严格来说,它不是数组。它可以用于数组可以使用的任何方式。如果它像鸭子一样嘎嘎作响,它就是鸭子。向量不会自动删除分配的存储,它会释放指针的存储空间,但不会释放指针指向的内存。使用boost::ptr_vector。您读过我的示例代码了吗?是否应该删除元素[2]指向的存储?如何将
Student*db[5]
Student**db=new Student*[5]相同
?使用一个函数比使用另一个函数有什么好处?当您希望从函数返回数组时,需要在堆上分配数组。如果在堆栈上分配数组,则会立即销毁该函数的作用域。这不增加任何内容。这不回答问题:在该代码中,您创建的是int数组,而不是数组这不是OP所问的。你能详细解释一下答案吗?
std::vector<Student*> db;
    void main()
    {
    int *arr;
    int size;
    cout<<"Enter the size of the integer array:";
    cin>>size;
    cout<<"Creating an array of size<<size<<"\n";
        arr=new int[size];
    cout<<"Dynamic allocation of memory for memory for array arr is successful";
    delete arr;
    getch();enter code here
}