std::multimap

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.
Please Help..
In the interest of not recreating the wheel, let me link you to a previous post I made about the same topic:
http://www.cplusplus.com/forum/general/102485/

EDIT:
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <map>
#include <iostream>

int main()
{
    //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
    mmap m;
    m.insert(mmap_pair("a", 1));
    m.insert(mmap_pair("a", 2));
    m.insert(mmap_pair("a", 3));
    m.insert(mmap_pair("b", 4));
    m.insert(mmap_pair("b", 5));
    m.insert(mmap_pair("c", 6));

    frequency_map freq;
    
    //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)
        freqs.insert(frequency_map::value_type(it->first, m.count(it->first)));
    
    //output results
    for(auto &i: freqs)
        std::cout << i.first << " has occurred " << i.second << " time(s) in m" << std::endl;
    
    return 0;
}

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


Hope that's what you're looking for.
Last edited on
could do it in a single pass, too:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <map>
int main()
{
    std::multimap<std::string, int> m = {{"a", 1}, {"a", 2}, {"a", 3},
                                         {"b", 4}, {"b", 5},
                                         {"c", 6}};
    for(auto i = m.begin(); i != m.end(); i = m.upper_bound(i->first))
        std::cout << i->first << " has occurred " << m.count(i->first) << " time(s) in m\n";
}
Thanks Cubbi. I hadn't thought of that.
Also, I forgot you could initialize maps with initializer_lists.
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.
for example:
in map there are two pairs:
("a",4) and("b",8)
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...
Topic archived. No new replies allowed.