删除2d数组中的列 [英] Remove a column in a 2d array

查看:101
本文介绍了删除2d数组中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想知道如果我仍然可以访问列中的第一行,例如我仍然可以访问数组的其余部分:

我的数组:1,2,3,4 5, 6,7,8

如果我释放3,我可以访问最后一列吗?

谢谢!

Hello, i was wondering if i freed the first line in a column if i could still access the rest of the array for example:
my array : 1,2,3,4 5,6,7,8
if i freed the 3 , could i access the last column?
thanks!

推荐答案

错误的想法。数组基于使用连续内存块,不适合删除。如果想要这样做,你需要重新分配内存,实际上,重写原始数组的一部分或全部。这是一种低效的操作,会破坏阵列效率的目的。因此,您应该更好地使用一些适合删除的其他数据结构,例如矢量或链表,与模板类 vector 或<$一样c $ c> list 在标准C ++库中找到:

http ://www.cplusplus.com/reference/vector/vector [ ^ ],

http://www.cplusplus.com/reference / list / list [ ^ ]。



例如,2D数组可以表示为矢量矢量,列表列表,列表矢量或矢量列表,具体取决于您的要求。事实上,这将产生锯齿状阵列,这对你的目的来说是好的。另请参阅: http://en.wikipedia.org/wiki/Jagged_array [ ^ ]。



我也想点由于C和C ++数组的性质,1D数组和2D数组完全相同。对数组元素的多索引访问只不过是语法糖 http://en.wikipedia。 org / wiki / Syntactic_sugar [ ^ ]。



-SA
Wrong idea. Arrays are based on using of continuous memory chunks and are not suitable for removing. If want to do this, you will need to reallocate the memory and, essentially, rewrite some part of original array or all of it. This is an inefficient operation which would defeat the purpose of the array's efficiency. Therefore, you should better use some other data structure suitable for deletion, such as vector or linked list, as with the template classes vector or list found in the standard C++ library:
http://www.cplusplus.com/reference/vector/vector[^],
http://www.cplusplus.com/reference/list/list[^].

For example, "2D arrays" can be represented as vectors of vectors, lists of lists, vectors of list or lists of vectors, depending on your requirements. In fact, that will make the "jagged arrays", which would be fine for your purposes. See also: http://en.wikipedia.org/wiki/Jagged_array[^].

I also want to point out that, due the nature of C and C++ arrays, "1D array" and "2D array" is exactly the same thing. The multi-index access to the array elements is nothing but syntactic sugar: http://en.wikipedia.org/wiki/Syntactic_sugar[^].

—SA


您的样本数组看起来不是二维的。



一般来说,除非它是指向动态分配内存的指针,否则不能释放任何内容,因此无法释放整数数组中的项。

如果您的意思是:我可以删除数组中的项吗?然后答案是是的,你可以做到但是这不简单(看看谢尔盖的回答)。
Your sample array doesn't look two-dimensional to me.

Generally speaking you cannot free anything unless it is a pointer to dynamic allocated memory, thus an item in a array of integers cannot be freed.
If you meant: "Could I remove an item in an array?" then the answer is "yes, you could do it" but it wouldn't be plain simple (have a look at Sergey's answer).


这篇关于删除2d数组中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆