从动态分配的数组中删除
Deleting from a dynamically allocated array
我有一个2D数组
char **m_data
这个数组的宽度为width
,高度由一个叫做m_heigths
的向量给出。
// Remove blank space where necessary
// Iterate through every row
for (int x=0; x<m_width; ++x){
// count number of spaces
int spaces=0;
// iterate through the given row
for (int y=0; y<m_heigths[x]; ++y){
// if space is occupied by a black space increment count
if (m_data[x][y]==' '){
++spaces;
}
}
// check if entire column is just a bunch of blanks
if (spaces==m_heigths[x]){
// get rid of blanks
delete [] m_data[x];
}
}
所以我想找一个只有一堆空格的列并删除它。但这似乎不起作用,空格留在那里。有人能帮我吗?
delete
只释放已分配的内存。要真正删除该行,您需要在调用delete之后将下面的所有行向上复制(在本例中,只需复制指针)1。就像Hayden在评论中说的,使用STL容器可能会更容易。
相关文章:
- 你能帮我解决删除数组 c++ 中重复值的挑战吗?
- 删除[i] 数组和删除数组 [i] 之间的区别
- 如何实现删除数组的功能?
- 删除数组 c++ 崩溃问题
- 如何删除数组列表中的元素
- C++删除数组只会解除分配第一项
- 删除数组成员的更好方法是什么?
- 如何使用 deleteEntry 函数 c++ 删除数组中的单个字符串
- 删除数组中所有出现的数字 C++
- C++ 删除数组时检测到堆损坏
- 不允许删除数组的一部分,为什么
- 如何删除数组中第一次出现的值
- 当我删除数组时,指针在哪里?指向数组的指针与数组中的元素之间的区别
- 尝试用C 中的空地址(而非null)删除数组指针
- 为什么 std::remove 不删除数组的最后一个元素
- 如何使用c++删除数组中的一些值
- 仅删除数组的第一个元素
- 删除数组时断言失败
- 如何并行删除数组中的零值
- 访问指针数组/通过另一个指针并删除数组中的单个元素而不删除第一个指针或整个数组