Iterating over `hash_multimap` by key and then by value
I need to execute some code on all the values of a key, and I need to repeated that action for every key. I look for something like:
for(auto key_iterator = hash_multimap.begin_keys();
key_iterator != hash_multimap.end_keys(); key_iterator++)
{
auto key = key_iterator->key;
// set up state
for(auto value_iterator = key_iterator->begin_values();
value_iterator != key_iterator->end_values(); value_iterator++)
{
// mutate state
}
// use state
// tear down state
}
This of course doesn't work, but is there a way to achieve similar effect? the problem is that I need to go over each key and then use a shared state for all of them. An example of what it can be used for is like:
typedef std::hash_multimap<int> hash_t;
typedef hash_t::value_type hash_val;
hash_t hash;
hash.insert(hash_val(0, 1));
hash.insert(hash_val(1, 2));
hash.insert(hash_val(1, 3));
hash.insert(hash_val(2, 4));
hash.insert(hash_val(2, 5));
hash.insert(hash_val(2, 6));
hash.insert(hash_val(3, 7));
hash.insert(hash_val(3, 8));
hash.insert(hash_val(3, 9));
// print out the sum of values for each key here.
开发者_如何学编程// expected output:
//
// 0: 1
// 1: 5
// 2: 15
// 3: 24
The problem with just using hash_multimap.begin()
is that I can't be sure it returns each key in consecutive block of that key, and even if it did, I can't know where such block begins and where it ends.
Edit: I also can't use hash_multimap.equal_range(key)
because I can't iterate over the keys. a way to iterate over keys that includes every key only once will solve this too.
How can I do this?
You can use lower bound and upper bound to achieve this. For example try -
auto value_iterator = hash->begin();
while( value_iterator != hash->end() ){
{
auto lIter = hash->lower_bound( value_iterator->first );
auto uIter = hash->upper_bound( value_iterator->first );
while( lIter != uIter ){
// sum the values associated with keys
// Increment lIter
}
value_iterator = uIter;
}
Edit: You can use equal_range
as @Thanatos suggested if the library you are using doesn't have upper_bound, lower_bound member functions. Microsoft implementation does actually have these. The logic is same and the inner loop would be -
pair<hash_multimap<int,int>::iterator,hash_multimap<int,int>::iterator> pairIter;
pairIter.equal_range(value_Iterator->first);
while( pairIter.first != pairIter.second ){
// sum the values associated with keys
// Increment pairIter->first
}
value_iterator = pairIter.second;
Since you used C++11 syntax, I will as well ;-]
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <iostream>
int main()
{
typedef std::unordered_multimap<int, int> map_t;
typedef std::unordered_map<int, int> sums_t;
map_t hash;
hash.insert(map_t::value_type(0, 1));
hash.insert(map_t::value_type(1, 2));
hash.insert(map_t::value_type(1, 3));
hash.insert(map_t::value_type(2, 4));
hash.insert(map_t::value_type(2, 5));
hash.insert(map_t::value_type(2, 6));
hash.insert(map_t::value_type(3, 7));
hash.insert(map_t::value_type(3, 8));
hash.insert(map_t::value_type(3, 9));
sums_t const& sums = std::accumulate(hash.cbegin(), hash.cend(), sums_t(),
[](sums_t& acc, map_t::const_reference p)
{
return acc[p.first] += p.second, acc;
}
);
std::for_each(sums.cbegin(), sums.cend(),
[](sums_t::const_reference p)
{
std::cout << p.first << ": " << p.second << '\n';
}
);
}
精彩评论