There is a vector. Suppose vec=[0.1,0.2,0.8,23.1,0.1,0.2] . There is a step, let's say h=0.1 from zero to m (in general, just the maximum element in the vector that is bypassed) you need to go through the whole vector to count how many times the element corresponds to the current step and write the total number to another vector. I did it like this:

  double j=0; B: int i=0; int n=0; for(j; j<=m; j=j+step) { A: for(i; i<=vec.size; i++) { if(vec[i]=j && i<vec.size()) { n++; goto A; } else(i==vec.size()) { vec1.pushback(n); goto B; } } } 

More than sure it's awful. Tell me, is there a more "competent" option?

  • go to A; and go to B; o_O - Qwertiy

1 answer 1

For example, something like this:

 std::vector<double> vec = { 0.1, 0.2, 0.8, 23.1, 0.1, 0.2 }; std::vector<int> result; const double m = 10; const double step = 0.1; for (double j = 0; j <= m; j += step) result.push_back(std::count_if(vec.begin(), vec.end(), [&](double v) {return std::fabs(v - j) < std::numeric_limits<double>::epsilon();})); 
  • Due to inaccuracies when comparing will give out vinigret - gbg
  • Yeah, it was necessary to google a little longer, even ashamed. thank you so much - bronstein87
  • @gdg agrees added error. - Vladimir Gamalyan
  • @Vladimir Gamalian still gives out some porridge - bronstein87
  • @ bronstein87 place for example your code on ideone.com so that you can run it, take a look. - Vladimir Gamalyan