lundi 17 décembre 2018

UnaryPredicate Function Definition

I am trying to create a histogram of a data vector std::vector<int> data of which elements are integers range from 0 to 255. I saw from an example code using std::count_if as follow:

int bins = 5; //number of intervals
std::vector<int> histogram(bins);
double interval = 255.0/bins; // interval length
for(int i=0; i< bins; i++){
    histogram.emplace_back(std::count_if(data.begin(),data.end(),
[i, interval](int j){return (j < ((double) i + 1) * interval && j >= (double) i * interval);}));
} 

I am confused about the last argument of the std::count_if function. It is supposed to be a UnaryPredicate function. I have a rough idea what the code is trying to do, but I do not understand the way the UnaryPredicate function is defined in the above example. Is this some short-handed way to define function? Is this common using this kind of definition or just limited similar problems?

Any explanation or link to similar problem/reading material will be appreciated.

Aucun commentaire:

Enregistrer un commentaire