c++ - 为多维字符串动态分配内存

标签 c++

如何为二维字符串动态分配内存,如下所示。

struct sample {
    int i;
    std::string arrray [10][10];
};

如何为下面的动态分配内存?

struct sample {
    int i;
    std::string **arrray;
};

最佳答案

第一个不需要内存分配(当您在结构中声明 C 样式数组时,编译将在结构内为它们分配空间)。

第二个是相当标准的二维数组。

arrray = new string*[10];
for (int i = 0; i < 10; i++)  
    arrray[i] = new string [10];  

关于c++ - 为多维字符串动态分配内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20158325/

相关文章:

java - OOP的缺点?

c++ - STL推回: previous values getting modified when pushing a dynamically changing array

c++ - 在 "2d-vector"(vector< vector<>>) 中创建 vector 大小的 vector ?

c++ - 如何在普通C++ 11中创建不同对象 “generators”的层次结构

c++ - 生成(不是这样)具有特定字符串出现的随机字符串

c++ - MATLAB:来自不同类的 OOP 调用函数

c++ - Qt延迟处理mousePressEvent

c++ - 在 C/C++ 中同时与 Openssl epoll 服务器通信到多个客户端

c++ - 使用C++确定三个数中最大的数

c++ - C++应用程序在VS2010和VS2008中的性能