如何使用多维数组c ++添加行和列 [英] How do I add rows and columns with multidimensional arrays c++

查看:83
本文介绍了如何使用多维数组c ++添加行和列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,对于每一行,但最后一个我希望最后一个元素是行中较早值的总和,对于每一列,我希望最底层的元素是它上面的那些值的总和。这是我的数组





const int ROWS = 5;

const int COLS = 6;

int ourNumbers [ROWS] [COLS] = {{17,34,7,8,2,0},

{2,4,82,2,11,0 },

{11,6,9,55,3,0},

{1,18,25,31,22,0},

{0,0,0,0,0,0}

};

解决方案

首先,我不要不知道为什么需要在数组中添加任何元素。这是在问题的标题中提出的,未经其正文确认。目前还不清楚为什么你不能只创建固定大小的所有元素。



但如果真正的问题是标题之一,我们假设你真的需要添加一些元素。



向已经初始化的数组中添加内容是一个坏主意,无论是否为二维,是否为锯齿状数组。这个维度不是真实的,与添加问题无关。可在此处找到所谓的多维数组的非常好的解释: http://www.cplusplus.com/doc/tutorial / arrays [ ^ ]。



如果你完全理解这个解释,你也可能会理解你不能简单地改变放置其他数组元素所需的内存大小。您需要重新分配数组,这通常基本上意味着使用原始数组中的元素副本创建一个全新的数组对象;所以重新分配可以是相当广泛的操作。这是因为阵列分配基于连续的内存位置。使用锯齿状阵列,图像有点复杂,但每个内部阵列仍然占用一个连续内存空间。因此,在所有情况下,您将需要额外的内存来添加元素,这通常可能不可用。



您需要使用不同的数据结构实现添加/插入/删除操作。当您使用C ++时,由于C ++标准库,这不是问题。特别参见 std :: vector std :: list

< a href =http://www.cplusplus.com/reference/vector/vector> http://www.cplusplus.com/reference/vector/vector [ ^ ],

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



另外,您可以使用链接列表

http://www.cplusplus.com/articles/LACRko23 [< a href =http://www.cplusplus.com/articles/LACRko23target =_ blanktitle =New Window> ^ ],

http://cslibrary.stanford.edu/103 [

href =http://cslibrary.stanford .edu / 103target =_ blanktitle =新窗口> ^
]。



这个CodeProject文章也很有用: http://www.codeproject.com / Articles / 24684 /如何创建链接列表使用CC



-SA

So for each row but the last I want to have the last element be the sum of earlier values in the row and for each column i want to have the bottom most element be the sum of those values above it. this is my array


const int ROWS = 5;
const int COLS = 6;
int ourNumbers[ROWS][COLS] = { { 17, 44, 7, 8, 2, 0},
{ 2, 4, 88, 2, 11, 0},
{ 11, 6, 9, 55, 3, 0},
{ 1, 18, 25, 31, 22, 0},
{ 0, 0, 0, 0, 0, 0}
};

解决方案

First of all, I don't see why would you need to add any elements to your array. This is asked in the title of the question and not confirmed by its body. It's unclear why you cannot just create all elements of fixed sizes.

But if the real question is the one of the title, let's assume you really need to add some elements.

It's a bad idea to add something to an array which is already initialized, no matter "two-dimensional" or not, jagged array or not. This dimension is not "real" and is majorly irrelevant to the problem of adding. Very good explanation of so called "multidimensional arrays" can be found here: http://www.cplusplus.com/doc/tutorial/arrays[^].

If you understand this explanation thoroughly, you may also understand that you cannot simply change the size of the memory needed to place additional array elements. You would need to reallocate the array, which, generally, essentially means creation of a brand-new array object with elements copies from the original array; so reallocation can be quite expansive operation. This because the array allocation is based on continuous memory location. With jagged array, the picture is a bit more complex, but each inner array still takes its one room of continuous memory. So, in all cases, you will need additional piece of memory for added elements, which is, generally, may be not available.

You need to use different data structures to implement add/insert/remove operations. As you are using C++, this is not a problem, due to C++ standard library. See, in particular, std::vector, std::list:
http://www.cplusplus.com/reference/vector/vector[^],
http://www.cplusplus.com/reference/list/list[^].

Also, you can use a linked list:
http://www.cplusplus.com/articles/LACRko23[^],
http://cslibrary.stanford.edu/103[
href="http://cslibrary.stanford.edu/103" target="_blank" title="New Window">^
].

This CodeProject article can also be useful: http://www.codeproject.com/Articles/24684/How-to-create-Linked-list-using-C-C.

—SA


这篇关于如何使用多维数组c ++添加行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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