hey Can anyone help me with multimap concept..
I am using multimap to store key-value pairs(duplicate keys are also present) during run time and after some time interval i want to count the number of values associated with each key.The value of Key is not known than what should i do to count the values.
I came back skimming over my topics a few moments ago and realized that I didn't really answer your question in my other post. I misread you the first time. (I read "the value of the key is known", sorry).
I'll address that now.
You can iterate over multimaps just like you can iterate over maps. Like std::map iterators, std::multimap iterators represent an std::pair<key_type, value_type> of the container. So you can iterate through the container and use std::multimap::count for each key, and insert it into a frequency map. For example:
//typedefs to make your life easier
typedef std::multimap<std::string, int> mmap;
typedef mmap::value_type mmap_pair;
typedef std::map<mmap::key_type, int> frequency_map;
//insert some values into m
//iterate over all values of m
//its okay to insert duplicate key results into freq because std::map will drop them.
for(auto &i: m)
for(auto &i: freqs)
std::cout << i.first << " has occurred " << i.second << " time(s) in m" << std::endl;
The output of the above program:
a has occurred 3 time(s) in m
b has occurred 2 time(s) in m
c has occurred 1 time(s) in m
thankyou so much guys... i had done that and its works..
Now in my code i want to use exponential moving average. Actually what i have to do is whenever the duplicate key arrives during run time the mapped value of that key should be replace or update by the average value.
in map there are two pairs:
and when again the key value pair ("a",6) comes
the value in the map should become ("a",5) which is the average of 4 and 6 and same thing for key "b". And for this i need to use exponential moving average and i am confused with that. Please help me.. Thanks in advance...