如何使用vector <pair <double,uint >>>为STL partial_sum实现binOp仿函数?

sas*_*cha 6 c++ stl

我想在向量中使用我的元素的partial_sum,其中每个元素都是一个pair<double, unsinged int>.所述partial_sum应该递增(第一各对)添加双值.

例:

vector<pair<double, unsigned int> > temp_vec;
temp_vec.push_back(make_pair(0.5, 0));
temp_vec.push_back(make_pair(0.2, 1));
temp_vec.push_back(make_pair(0.3, 2));
partial_sum(temp_vec.begin(), temp_vec.end(), temp_vec.begin(), ???);   // in place
Run Code Online (Sandbox Code Playgroud)

应该给我一个包含:[(0.5,0),(0.7,1),(1.0,2)]的向量

如何实现必要的仿函数来使用partial_sum函数?

我能够使用自定义函数在stl lower_bound搜索中使用我的对,但在上面的例子中,我不知道如何声明二进制操作.

Bjö*_*lex 5

struct pair_sum {
    pair<double, unsigned int> operator()(const pair<double, unsigned int> & sum, const pair<double, unsigned int> & i) {
        return pair<double, unsigned int>(sum.first + i.first, i.second);
    }
};
Run Code Online (Sandbox Code Playgroud)

这将加起来first并返回seconds不变.