多图累积值 [英] multimap accumulate values

查看:102
本文介绍了多图累积值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由

typedef std::pair<int, int> comp_buf_pair; //pair<comp_t, dij>
typedef std::pair<int, comp_buf_pair> node_buf_pair;
typedef std::multimap<int, comp_buf_pair> buf_map; //key=PE, value = pair<comp_t, dij>
typedef buf_map::iterator It_buf; 
int summ (int x, int y) {return x+y;}


int total_buf_size = 0;
std::cout << "\nUpdated buffer values" << std::endl;
for(It_buf it = bufsz_map.begin(); it!= bufsz_map.end(); ++it)
{
    comp_buf_pair it1 = it->second;
    // max buffer size will be summ(it1.second)
    //total_buf_size = std::accumulate(bufsz_map.begin(), bufsz_map.end(), &summ); //error??
    std::cout << "Total buffers required for this config = " << total_buf_size << std::endl;
    std::cout << it->first << " : " << it1.first << " : " << it1.second << std::endl;

}

我想对它指出的所有值求和1。第二个
std :: accumulate函数如何访问第二个迭代器值?

I would like to sum all the values pointed by it1.second How can the std::accumulate function access the second iterator values?

推荐答案

问题在于summ函数,实际上您需要比它更好的东西才能处理2个不匹配的类型。

Your issue is with the summ function, you actually need something better than that to be able to handle 2 mismatched types.

如果幸运的话,这可能会起作用:

If you're lucky, this could work:

int summ(int x, buf_map::value_type const& v) { return x + v.second; }

如果您倒霉(取决于的累积方式已实施),您总是可以:

If you're unlucky (depending on how accumulate is implemented), you could always:

struct Summer
{
  typedef buf_map::value_type const& s_type;
  int operator()(int x, s_type v) const { return x + v.second.first; }
  int operator()(s_type v, int x) const { return x + v.second.first; }
};

然后使用:

int result = std::accumulate(map.begin(), map.end(), 0, Summer());

这篇关于多图累积值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆