简体   繁体   English

在C ++中是否可以有一个行大小不同的矩阵?

[英]Is it possible to have a matrix with different row sizes in C++ ?

I need to read from a file some ints. 我需要从文件中读取一些整数。 The first int indicates how many ints I will have in that line. 第一个int指示该行中将有多少个int。

Example: 例:

5 1 4 7 10 2
4 5 6 7 1
3 1 0 8
3 4 5 6
4 1 2 3 4

Is there a way to create a matrix like this? 有没有办法创建这样的矩阵?

I saw that there's a way of doing something similar but it isn't with matrix: Is it possible to make a matrix with rows that have different #rows on Matlab? 我看到有一种方法可以执行类似的操作,但不能与矩阵一起使用: 是否可以在Matlab上用行数不同的矩阵制作矩阵?

You want a jagged edged matrix. 您需要锯齿状的边缘矩阵。

In mathematical modelling this is normally a most undesirable thing indeed, but in your particular case, a 在数学建模中,这确实通常是最不可取的,但是在您的特定情况下,

std::vector<std::vector<int>>

will model this perfectly. 会完美建模。 Be mindful that the properties of an int vary from platform to platform. 请注意, int的属性因平台而异。 The minimum range an int can take is -32767 to + 32767. Consider using a long &c. 一个int可以接受的最小范围是-32767到+32767。考虑使用long &c。 if necessary. 如有必要。

It is not possible to have 2-d arrays with different row sizes in C++ (If this is what you mean by matrix). 在C ++中,不可能有具有不同行大小的二维数组(如果这是矩阵的意思)。 You can use some C++ std library container or some workaround to overcome this limitation. 您可以使用某些C ++ std库容器或某些解决方法来克服此限制。 Some options are: 一些选项是:

  • Array of pointers where each pointer points to an array (usually dynamically allocated) of required size. 指针数组,其中每个指针都指向所需大小的数组(通常是动态分配的)。
  • Array of std::vector or other container. std::vector或其他容器的数组。 Or std::vector , std::array or other container of std::vector . std::vectorstd::array或其他容器std::vector (For ex: std::vector< std::vector< T > > ) (例如: std::vector< std::vector< T > >
  • 2-d array with max row size. 具有最大行大小的二维数组。

In all of the above approaches data of rows is not contiguous. 在所有上述方法中,行的数据都不连续。 To make data contiguous (if required), you may create a class of your own which creates a big 1-d array and emulate it as a 2-d array. 为了使数据连续(如果需要),可以创建自己的类,该类创建一个大的一维数组并将其模拟为二维数组。

Matrices are by definition rectangular. 根据定义,矩阵是矩形的。 So, what you are describing is not a matrix. 因此,您所描述的不是矩阵。 It is a jagged array. 这是一个锯齿状的数组。

You cannot have an array of arrays where the subarrays are different sized. 您不能拥有子数组大小不同的数组数组。 All elements of an array must have the same type. 数组的所有元素必须具有相同的类型。 But you can have an array of pointers, which can point to arrays of any size. 但是您可以拥有一个指针数组,该指针数组可以指向任何大小的数组。 A simplest solution is to use std::vector : 最简单的解决方案是使用std::vector

std::vector<std::vector<int>> vec_of_vecs;

This is actually the simplest way to represent a 2 dimensional array structure (jagged or not), although the memory representation isn't contiguous like you would expect from a non jagged 2D array. 实际上,这是表示二维数组结构(有锯齿或无锯齿)的最简单方法,尽管内存表示并不像非锯齿2D数组那样是连续的。


For performance, it is possible to store the arrays in a single memory block as well, but that is going to be much more complicated. 为了提高性能,也可以将阵列存储在单个存储块中,但这将变得更加复杂。 What you can do, is to allocate a single block of memory that contains all of the rows, and another array to store pointers to beginnings of each row. 您可以做的是分配一个包含所有行的内存块,并分配另一个数组来存储指向每行开头的指针。

It is possible to create a std::vector<std::vector<int> > ie a std::vector with elements of type std::vector<int> . 可以创建一个std::vector<std::vector<int> >即一个std::vector ,其元素类型为std::vector<int>

The individual std::vector<int> s can be of different sizes. 各个std::vector<int>的大小可以不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM