Step-by-step vector and cell count



  • There's a vector. All right. vec=[0.1,0.2,0.8,23.1,0.1,0.2]♪ There's a step. h=0.1 from zero to zero m(in general, the maximum of the vector that is circumvented) needs to be taken all over the vector and count as many times the element of the relevant current step and write the total number into another vector. I did 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 I'm sure it's awful. Can you tell me if there's a more graceful option?



  • 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();}));


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2