I have come across this reference article on the count function:(http://www.cplusplus.com/reference/algorithm/count/) And while testing it, I could not find how to use the function on multiple dimensional arrays.
This is the way I am testing it, with a one dimensional array:
1 2 3 4 5 6 7 8 9 10 11 12
#include<iostream>
#include<algorithm>
usingnamespace std;
int main() {
int myArray[3] = {2, 3, 2};
int myCount = count(myArray, myArray+3, 2);
cout << "The number 2 appears " << myCount << " times";
}
And this is the way I tried to use it on the two dimensional array:
1 2 3 4 5 6 7 8 9 10 11 12
#include<iostream>
#include<algorithm>
usingnamespace std;
int main() {
int myArray[3][3] = {{2, 3, 2}, {2, 3, 2}};
int myCount = count(myArray, myArray+6, 2);
cout << "The number 2 appears " << myCount << " times";
}
The second code gives me an error in the file "stl_algo.h". I assume that file has to do with the <algorithm> library, but I have no idea how to fix it, or if there is other way to check for repetition on a two dimensional array.
myArray have type of int**. When dereferenced (in count()) it becomes int*. You just cannot compare pointer and integer.
What you can do is dereference pointer befor passing it: int myCount = count(*myArray, *(myArray+6), 2);However it is probalbly causes an undefined behavior (it depends on whether standard specifies how multidimension array should be placed in memory)
It surely will not work on dynamically allocated arrays.
So you might want to search for alternative solutions.