Reputation: 959
How can I determine the size of a 2-Dimensional array in C++ when each row and column size may vary?
I'm trying to make a function called Parser
that reads the array. So far, I have:
// Function: Parser.
// Description: First, it reads the data in the array.
// Then, it uses the data in the array.
void Parser (char ch[][]) {
for (int i = 0; i < (sizeof (ch) / sizeof (ch [0])); ++i) {
// TO DO - Add content.
}
}
Array ch
can contain elements such as:
{
{
'v', 'o', 'i', 'd'
},
{
'i', 'n', 't'
},
}
Is there a solution?
Upvotes: 1
Views: 340
Reputation: 959
What I did was, I made ch
a vector
. I passed ch
's size to iterator i
in the first for
loop, then ch[i]
's size to iterator j
.
The new code is:
// Function: Parser.
// Description: First, it reads the data in the vector.
// Then, it uses the data in the vector.
void Parser (vector <vector <char>> ch) {
for (int i = 0; i < ch.size (); ++i) {
for (int j = 0; j < ch[i].size (); ++j) {
// TO DO - Add content.
}
}
}
Upvotes: 0
Reputation: 536
There are Many ways
Use Templates:
template <typename T, size_t N, size_t M>
void Parser (T (&ch)[N][M])
{
}
Upvotes: 4