C++ 创建std::string的二维数组的最佳实践

C++ 创建std::string的二维数组的最佳实践,c++,multidimensional-array,C++,Multidimensional Array,因为这是一个学习练习,所以我忽略了使用向量的想法(目前) 我编写了一个程序,创建一个小的二维数组,添加数据,然后删除二维数组,并尝试使用最佳实践 有人能提出改进建议吗?我最关心的是在使用完2d阵列后清理它,这样可以避免内存泄漏的可能性 提前谢谢 #include <iostream> #include <string> using namespace std; void DIM(std::string **marray,unsigned int row,unsigne

因为这是一个学习练习,所以我忽略了使用向量的想法(目前)

我编写了一个程序,创建一个小的二维数组,添加数据,然后删除二维数组,并尝试使用最佳实践

有人能提出改进建议吗?我最关心的是在使用完2d阵列后清理它,这样可以避免内存泄漏的可能性

提前谢谢

#include <iostream>
#include <string>

using namespace std;

void DIM(std::string **marray,unsigned int row,unsigned long cols)
{
    if (marray[row] != nullptr) {
        delete [] marray[row];
        marray[row] = nullptr;
    }
    marray[row] = new std::string[cols];
}

void DIM_DELETE (std::string **marray,unsigned int row)
{
    if (*&marray[row] != nullptr) {
        delete[] marray[row];
        marray[row] = nullptr;
    }
}

void DIM_DELETE_ALL (std::string **marray,unsigned int rows)
{

    for (int i=(rows-1); i>-1; i--) {
        if (marray[i] != nullptr) {
            delete[] marray[i];
            marray[i] = nullptr;
        }
    }//next i
//now take care of marray
    if (marray != nullptr) {
        delete [] marray;
        marray = nullptr;
    }
}

std::string **create2darray(unsigned int rows,unsigned int cols)
{
//first create the pointer
    std::string **my = nullptr; //create pointer , note: no data portion assigned yet

//now assign a data portion to the pointer (could do above in one step)
    my = new std::string*[rows];// elements 0 through rows-1 //assigns data section (an array of std::strings)

//now set newly created rows to nullptr
    for (unsigned int i = 0; i<rows; i++) {
        my[i] = nullptr;
    }

//dim each row for cols columns
    for (unsigned int i = 0; i<rows; i++) {
        DIM(my,i,cols);//dims the strings (creates data portion) my = new std::string*[x];//
    }

    return my;//returning a std::string **
}

int main()
{

    unsigned int rows = 3;//3 rows (0 through 2)
    unsigned int cols = 4;//4 columns (0 through 3)

    std::string **myarray = create2darray(rows,cols);//2d array (3 rows, 5 cols)
    cout << "2d array created" << endl << endl;
    myarray[0][0] = "a0"; //row 0, col 0
    myarray[1][0] = "b0"; //row 1, col 0
    myarray[2][0] = "c0";
    myarray[0][1] = "a1";
    myarray[0][2] = "a2";
    myarray[0][3] = "a3";
    myarray[1][1] = "b1";
    myarray[1][2] = "b2";
    myarray[1][3] = "b3";
    myarray[2][1] = "c1";
    myarray[2][2] = "c2";
    myarray[2][3] = "c3";
    cout << "assigned data to rows 0 to 2 and cols 0 to 3" << endl << endl;
    for (unsigned int i=0; i<rows; i++) {
        cout << i << ",0: " << myarray[i][0] << " " << i << ",1: " << myarray[i][1] << " " << i << ",2: " << myarray[i][2] <<  " " << i << ",3: " << myarray[i][3] << endl;
    }
    cout << endl;
    cout << "we are done with 2d array, let's delete it" << endl;
//tested dim_delete (seems to work)
    /*
    DIM_DELETE(myarray,0);//delete [] myarray[0]; myarray[0] = nullptr;
    DIM_DELETE(myarray,1);
    DIM_DELETE(myarray,2);
    //still need to delete myarray
    delete [] myarray;
    myarray = nullptr;
    */

//delete all rows and delete the std::string that holds the rows
    DIM_DELETE_ALL(myarray,rows);
    cout << "array deleted, all done" << endl;

//hold cursor so user can see console messages
    do {} while(cin.get()!='\n');
    return 0;

}
#包括
#包括
使用名称空间std;
void DIM(标准::字符串**marray,无符号整数行,无符号长列)
{
如果(marray[世界其他地区]!=nullptr){
删除[]marray[世界其他地区];
marray[世界其他地区]=空PTR;
}
marray[row]=新标准::字符串[cols];
}
void DIM_DELETE(标准::字符串**marray,无符号整数行)
{
如果(*&marray[row]!=nullptr){
删除[]marray[世界其他地区];
marray[世界其他地区]=空PTR;
}
}
void DIM\u DELETE\u ALL(标准::字符串**marray,无符号整数行)
{
对于(int i=(行-1);i>-1;i--){
如果(marray[i]!=nullptr){
删除[]marray[i];
marray[i]=nullptr;
}
}//接下来我
//现在照顾好玛丽
如果(marray!=nullptr){
删除[]玛丽;
marray=nullptr;
}
}
std::string**create2darray(无符号整数行、无符号整数列)
{
//首先创建指针
std::string**my=nullptr;//创建指针,注意:尚未分配任何数据部分
//现在为指针分配一个数据部分(可以一步完成以上操作)
my=new std::string*[rows];//元素0到rows-1//分配数据段(std::string的数组)
//现在将新创建的行设置为nullptr

对于(unsigned int i=0;i如果数组的第二维是常量,我认为可以使用单个new和delete运算符

#include <stdio.h>
#include <string>

int ROWS = 3;
const int COLS = 4;

typedef std::string arr4[COLS];

int main()
{
        arr4 *a;

        a = new arr4[ROWS];

        for (int i = 0; i < ROWS; i++)
          for (int j = 0; j < COLS; j++)
            a[i][j] =  std::to_string(j*ROWS + i);

        for (int i = 0; i < ROWS; i++)
        {
          for (int j = 0; j < COLS; j++)
             printf("%s ", a[i][j].c_str());
          printf("\n");
        }
        printf("\n");

        delete [] a;
}
#包括
#包括
int行=3;
常数int COLS=4;
typedef std::字符串arr4[COLS];
int main()
{
arr4*a;
a=新arr4[行];
对于(int i=0;i
我建议你不要使用
new
。而是使用
std::unique\u ptr
std::make\u unique
,或者使用
std::vector
。如果你需要一个二维数组,制作一个内部有一个一维
std::vector
行*列的类,并制作一个
std::string操作符()const
getter。避免内存泄漏的最佳方法是在逻辑上不可能泄漏内存。如果没有新的
new
ed内存,那么在逻辑上就不可能泄漏内存,你同意吗?在没有内存泄漏的情况下很难泄漏内存。如果是这样,那么只需更换所有的
new
s和
delete
s with
std::vector
,它为您处理所有的内存分配,这样您就不必担心任何令人讨厌的内存泄漏。毕竟这就是
std::vector
的全部内容。这就是它的工作。如果您真的想手工完成这项工作,我建议您花时间编写一个正确构建的、自制的
vector
class,最好是一个模板类。以编写代码的方式编写代码基本上什么也学不到,因为没有人(至少有经验的人)以这种方式编写代码。至少,如果您创建自己的类,您正在学习如何正确分配/释放内存,如何编写具有正确复制语义的类,等等。否则,如果你想跳过所有这些,然后使用
std::vector
,就像其他人所说的那样。