Manipulating one of the values of a vector of pairs in C++

If I have a vector of doubles PMF I can divide all elements of the vector by a double count by using the transform command as follows:

transform(PMF.begin(),PMF.end(),PMF.begin(),bind2nd(divides<double>(),count));

Now however, I have a vector of unsigned char/double pairs:

vector<pair<unsigned char, double>> PMF

I wish to replace the double values by their values divided by count. I haven't been able to find a way to do this using the transform command or any other C++11 functionality. Does anyone have an idea as to how to do this?

Upvotes: 0

Views: 206

Answers (1)

masoud
masoud

Reputation: 56509

You can use a lambda function as same as this:

transform(PMF.begin(),
          PMF.end(),
          PMF.begin(),
          [count](const pair<unsigned char, double> &x)
          {
             return make_pair(x.first, x.second/count);
          });

or

for_each(PMF.begin(),
         PMF.end(),
         [count](pair<unsigned char, double>& x)
         {
            x.second /= count;
         });

or

for (auto &x : PMF)
  x.second /= count;

Upvotes: 3

Related Questions