Find each length of a multidimensional array

Pages: 12
Lets take int var[size1][size2] in a single dimensional array I can find the size by using .length() but how do I find "size1" and "size2" individually?
Last edited on
Please show how you can get the size of one-dimensional array by means of length()? I am asking this question because it is not clear what array you mean.
int var[] = {1, 2, 3};
cout << var.length();

Are you sure that you are using C++?:)
In C++ arrays have no methods.:) Maybe you meant

std::array<int, 3> var = { 1, 2, 3 };
Last edited on
I am using C++ and I know that length should be a function belonging to arrays (unless I'm getting a little confused with a string)
What does the size() member function of an array return, I have a feeling it returns the memory use by the computer to store the information in the array?
Definetly getting confused with strings (for which lenght() and size() do exactly the same thing). IIRC java arrays have lenght().

C++ offers std::vector and, since C++11, std::array
Last edited on
Java arrays have .length - it is not a method (it is not .length() )

In C++, plain arrays allocated on the stack or with new do not remember their size - you must remember it for them.
I am using C++ and I know that length should be a function belonging to arrays (unless I'm getting a little confused with a string)

When I said "show how you can get the size of one-dimensional array by means of length()" it means that you shall write the corresponding code and try to compile it.
Please to not bother the forum any more if you even do not want to do such simple checking.
Last edited on
My apologies but what appears to be so simple is actually a tiny part of a much, much bigger picture.
I'm using an array of "Item" classes (my own define class) for a players inventory in a game, the "Player" class is a subclass of a "Mob" class, it's in the Mob class where the Item array is defined for a Mobs inventory...
Some items (such as armor) will give Mobs effects (such as defence) and whenever something in the inventory array changes I need to update Mob stats or abilities based on items in the inventory and I'd usually use a for loop with length (turns out its actually size but oh well) to prevent me trying to access out of bound members, then I realised with a multidimentional grid I didn't know how to find the max points.
Use std::vector.
/Problem solved.
But still how do I get the length of each dimension?
Sorry if this does seen really obvious, my mind's been destroyed by other various things that are are actually complicated.
A 2d array is of size = length x width. At some point YOU are setting these sizes, whether they be dynamically or statically. So I don't see the issue you're having here.
The main Mob class will not know the sizes of the inventories in any inherited classes such as the Player class and therefore will not be able to perform functions involving the inventory properly without being able to fund out how large the inventory is... I do suppose 2 simple integers in inherited classes holding the length and width could work but I'm an efficiency freak and if I could get away with saving memory somehow I will (2*char*number_of_mobs, memory will be used for this method, I know if isn't a lot but I also don't like having variables for the sake of it if there is other ways)
The base Mob class should not even know what a Player is, let alone what the player's inventory is.
well, one way to find an array length (without using function), is this:

int arr [10][5];

cout << "row size is: " << sizeof (int) * 10 << endl;
cout << "column size is: " << sizeof (int) * 5 << endl;

@chipp I think you misread; he wants the number of elements because he does not know the number of elements.
But he should know the number of elements. OP, you're either going to have to store the size of the array somewhere, or use vector (which is going to do it anyways). The overhead is negligible. Unless you're working with a microcomputer with 2 MB RAM, an extra variable is nothing to worry about.
But he should know the number of elements

yes, i think the same. there's no way he wouldn't know the number of elements:
1. if it's defined by himself, of course he would've know the size
2. if it's user-defined array size, then the user's value would've been stored on a variable

Right I am just going to add an couple of ints for this.
And no, I wouldn't always know the size of the inventories as I have explained. Lets say a player has and inventory grid of 9x4, and perhaps a boss that stores special loot to drop when he's defeated might keep 5 items in a 5x1 grid... The functions to manipulate inventories will be in the base Mob class an therefore will not know the inventory sizes of the sub-classes the functions will be executed in... We could also say that the player could have a backpack as an item that adds and extra 5x3 slots or such, again making the functions unable o wok properly because they don't know the size of the array grid
If you really insist on doing this without a vector you can use a 1D array like
int var[row * col];
int size = sizeof(var)/sizeof(int);

Pages: 12