【发布时间】:2010-06-24 17:08:46
【问题描述】:
我有一个由
定义的多图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.second 指向的所有值相加 std::accumulate 函数如何访问第二个迭代器值?
【问题讨论】:
-
嗨,如果你仍然在迭代它们,为什么不把它添加到你的循环中呢?
total_buf_size += it1.second; -
it1.second 中的“所有值”是什么意思? It1.second 只是一个整数。里面只有价值。
-
@ufotds 有时最简单的步骤就可以解决问题。我用了你提到的
total_buf_size += it1.second;。我在尝试使用基于向量容器的 STL 示例的累积时受到影响。我也试图避免循环通过容器。 -
@Rob 我的意思是 it1.second 所指向的所有值的总和
标签: c++ stl multimap accumulate