質問

で定義されたマルチマップがあります

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;

}

it1.econdで指摘されたすべての値を合計したいと思います。STD::蓄積関数は2番目のイテレーター値にどのようにアクセスできますか?

役に立ちましたか?

解決

あなたの問題はSUMM機能にあります。実際には、2つの不一致のタイプを処理できるようにするために、実際にそれよりも良いものが必要です。

運が良ければ、これはうまくいくかもしれません:

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

あなたが不運な場合(方法によって異なります accumulate 実装されています)、あなたはいつでも:

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

他のヒント

私はあなたがあなたを変える必要があると思います summ 代わりにマップvalue_typeを取得する機能。これは完全にテストされていませんが、アイデアを与えるはずです。

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

そしてそれを呼びます:

total_buf_size = std::accumulate(bufsz_map.begin(), bufsz_map.end(), 0, &summ);

なぜあなたはペアを含むペアを台無しにするのですか?それは複雑すぎて、あなたはエラーを起こすことになります。構造体を定義してみませんか?

Accumulate 合計の一般化です。 init 範囲内のすべての要素 [first, last).

...結果は最初に初期化されます init. 。次に、各イテレーターに対して i[first, last), 、最初から最後まで、それはによって更新されます result = result + *i (最初のバージョン)または result = binary_op(result, *i) (2番目のバージョン)。

sgi.com

あなたの試みは最初のバージョンでも2番目のバージョンでもありませんでした、あなたはinitパートを逃しました

total_buf_size = std::accumulate(bufsz_map.begin(), bufsz_map.end(), 0, &summ);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top