C++ <Map> find the repeated value!

I have below program in C++. All input passing by command line argument. In here the key is string as word and Value is an int as number of frequency.

1) If the “value” of map repeated (duplicate), if so then and printout the “There is duplicated”

Example:

bag 2

bad 2

girl 2

We have to see if the frequnct is repeted. It stored already in the value of map.

2) What is most highest frequency in the map and printout the message.




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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
 #include <fstream>
#include <string>
#include <iostream>
#include <map>

using namespace std;

int main (int argc, char *argv[])

{

......

for (int i = 5; i < argc; i++)

{

// Open the file

{

return 0;

}

}

}

for (int i = 5; i < argc; i++){

ifstream file (argv[i]);

string dataIn;

map < string, int > myMap;

// split words from input

while (file >> input)

{

// inc. count

}

map::iterator it;

for (it = myMap.begin(); it != myMap.end(); it++) {

// first is key AND second is value

cout << it->first << " " << it->second << endl;

// More code need to be added...

}

}

}
Last edited on
closed account (SECMoG1T)
std::multimap would be your way to go if you want to store duplicates keys from where you can proceed to find the frequencies.
We can use a second auxiliary map, where the key is the frequency and the mapped value is the number of occurrences of this frequency. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <string>
#include <map>

void print_frequencies( const std::map< std::string, int >& map )
{
    // key: frequency, mapped value: number of strings with his frequency
    std::map<int,int> aux_map ;

    for( const auto& pair : map ) ++aux_map[pair.second] ;
    const int max_frequency = !aux_map.empty() ? aux_map.rbegin()->first : 0 ;

    for( const auto& pair : map )
    {
        std::cout << pair.first << ' ' << pair.second ;
        const int num_times = aux_map[pair.second] ;
        if( num_times > 1 ) std::cout << "  (there are " << num_times << " strings with this frequency)" ;
        if( pair.second == max_frequency ) std::cout << "  (this is the highest frequency)" ;
        std::cout << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/2e1ad64e6f183f09
Thanks for your reply "JLBorges", But I'm not familiar with void. May i ask you how i can implement your code into my code?

Thanks
> But I'm not familiar with void.

Does that mean that you are not familiar with writing a function?
How to implement in my above code! I never use the void!
I can see that you used out of function and call the function within the int()

All input passing by argument line..
Last edited on
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
33
#include <fstream>
#include <string>
#include <iostream>
#include <map>

int main (int argc, char *argv[])
{
    ///////////////////////////////////////////////////////////////////////
    // ...

    std::map< std::string, int > myMap ;

    // open files, read all the stuff, updating myMap as you had done

    ////////////////////////////////////////////////////////////////////////

    // finally, right at the end:

    // key: frequency, mapped value: number of strings with his frequency
    std::map<int,int> aux_map ;

    for( const auto& pair : myMap ) ++aux_map[pair.second] ;
    const int max_frequency = !aux_map.empty() ? aux_map.rbegin()->first : 0 ;

    for( const auto& pair : myMap )
    {
        std::cout << pair.first << ' ' << pair.second ;
        const int num_times = aux_map[pair.second] ;
        if( num_times > 1 ) std::cout << "  (there are " << num_times << " strings with this frequency)" ;
        if( pair.second == max_frequency ) std::cout << "  (this is the highest frequency)" ;
        std::cout << '\n' ;
    }
}
Thanks, I'm getting warning...
Warning - the output rate '567686.0000 bytes/s' has exceeded the allowed value '6000 bytes/s'; attempting to kill the process.
Last edited on
Another question that i have here is the Key is word and should be in string and value is number that should be in int...
Topic archived. No new replies allowed.