get current quantity of rows of dynamic 2d array

have 2d array

int array_of_ints [1000][1];
this array will be dynamically filled with data
need get current quantity of rows of dynamic 2d array
not size of array storage cells, but exactly how many not null rows do I have
for example it filled by 20 rows, I need to get this quantity with some request






There are no "null rows". Each "row" is just an array.

What you have there is technically an "array of arrays". An array with 1000 elements where each element is an array containing one int.

It sounds like perhaps you want to use a different data structure. If you just want a resizeable array you could use std::vector. If you want to allow "empty" elements you could use std::optional.
Last edited on
You may use another variable like

int array_rows_used = 0;

When you fill the rows you need to update that variable.
[1] is a useless dimension. its exactly the same as the 1d array.
its like saying
{0,1,2} is a 2-d array and we just didn't happen to use the other rows (or columns, if you want to read it that way).
Last edited on
I know its 9 rows now, but I dont have variable to know that
How can I get the quantity of rows with request?
I dont use vectors, I just use simple C array[1000]
Last edited on
I know its 9 rows now, but I dont have variable to know that

With a C++ vector there is no need to have a separate variable, just use the container's size() member method. So you don't have to know what the size of the container is until you have a need to know.

I dont use vectors, I just use simple C array[1000]

Unless you write C code you should use a C++ container. C++ has fixed and dynamic sized containers. Managing the dimension(s) and memory for you.

std::array is a bit of a peculiar duck compared to the other containers, a wrapper around a C style array so it has a common interface with the other containers.

Using a C array will complicate the code needed to programmatically figure out the number of entries, either rows or columns. Especially complicated is passing a 2D array into a function, the array devolves to a pointer making it hard to know the sizes without passing them as parameters.

Or some other scheme that uses 'sentinel values,' a C string uses the NULL character, '\0', for that.

A C++ container retains the ability to query the size(s) of the container after being passed into a function.

With a 2D std::vector it is easy to query the number of of rows, and the number of entries/columns in each row, using vector's member size() method. Using std::vector makes it easy to have a 'ragged' 2D layout. Each row potentially can have a differing number of columns.
you do it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct reinventedwheel
{
  int rows{};
  int cols{};
  type ** data{};  
  ...etc
};

int main()
{
  reinventedwheel x;
  ... blah blah

  //hey, how many rows we got?
  cout << x.rows;
}


if you insist on using arrays, its even easier:
const int rows = 42;
const int cols = 101;
type x[rows][cols];
...
cout << rows; //you have this many rows.
if you go all 1985 C on it, and allocate a big array that you use part of, then
you also just have a used_rows and used_cols variable that you keep track of, and when you want to know the value, its right there. Extra credit for keeping it in a struct, at least, so you have a little organization intead of globals or something.
Last edited on
How can I get the quantity of rows with request?
A less efficient approach would be to fill the array with invalid values (if there is one) beforehand.
Each time you need to know the size you count the valid values until the invalid.
That's similar to a c-string where '\0' is the invalid value.
Topic archived. No new replies allowed.