How to count same values in array c++ -
i trying count number of same elements on net, found easy solution on net adjusting understand myself. trying count how many number of 40 there in array. 2.
#include <iostream> #include <algorithm> #include<array> using namespace std; int main(){ int array[6] = {38,38,40,38,40,37}; cout<<count(ca.begin(),ca.end(),40); return 0; }
the error ca not recognised, , here codes found work counting. http://www.cplusplus.com/forum/general/154188/
the example have linked using std::array
called ca
. takes type , number of elements, std::array<int, 6>
expects 6 int
s , fixed length array.
this has begin
, end
method , plays nicely algorithms in stl.
if have c style array instead, can use std::begin
, std::end
achieve same thing.
array<int, 6> ca{ 38,38,40,38,40,37 }; cout << count(ca.begin(), ca.end(), 40) << '\n'; int c_style_array[] = { 38,38,40,38,40,37 }; cout << count(std::begin(c_style_array), std::end(c_style_array), 40) << '\n';
Comments
Post a Comment