Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 删除字符串数组_C++ - Fatal编程技术网

C++ 删除字符串数组

C++ 删除字符串数组,c++,C++,假设我有一个字符串数组: string*项 这个项数组是使用new操作符动态构造的。如果数组中的条目数为numItems,如何释放动态分配的内存?使用new[]分配数组,使用delete[]释放数组: #include <string> std::string* item = new std::string[numItems]; ... delete[] item; 使用new[]分配数组,使用delete[]释放数组: #include <string> std:

假设我有一个字符串数组:

string*项


这个
数组是使用
new
操作符动态构造的。如果数组中的条目数为
numItems
,如何释放动态分配的内存?

使用
new[]
分配数组,使用
delete[]
释放数组:

#include <string>

std::string* item = new std::string[numItems];
...
delete[] item;

使用
new[]
分配数组,使用
delete[]
释放数组:

#include <string>

std::string* item = new std::string[numItems];
...
delete[] item;

为什么不使用
std::vector
?@RemyLebeau对整个数组使用单个delete[],或者我应该循环遍历每个元素并使用delete[]?您必须使用下标运算符
[]
进行删除<代码>删除[]项。但是如果数组是
std::vector
,那么向量的析构函数通过调用vector@Naveen您没有
new
'使用单个字符串,只有
new[]
'使用数组,因此严格来说只需要1个
delete[]
string*项
不是数组。它是一个指针。为什么不使用
std::vector
?@RemyLebeau对整个数组使用单个delete[],或者我应该循环遍历每个元素并使用delete[]?您必须使用下标运算符
[]
进行删除<代码>删除[]项。但是如果数组是
std::vector
,那么向量的析构函数通过调用vector@Naveen您没有
new
'使用单个字符串,只有
new[]
'使用数组,因此严格来说只需要1个
delete[]
string*项
不是数组。这是一个指针。