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?