-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordcount.cpp
More file actions
60 lines (43 loc) · 1.43 KB
/
Copy pathwordcount.cpp
File metadata and controls
60 lines (43 loc) · 1.43 KB
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
/****************************************************************************/
// Author- Ayden Mason
// May 9th 2019
// Counting instances of words in a file.
// C++
/****************************************************************************/
#include <iostream>
#include <map>
#include <iterator>
#include <string>
#include <algorithm>
using namespace std;
template <typename A, typename B>
multimap<B, A> flip_map(map<A,B> & src) {
multimap<B,A> dst;
for(typename map<A, B>::const_iterator it = src.begin(); it != src.end(); ++it)
dst.insert(pair<B, A>(it -> second, it -> first));
return dst;
}
int main(int argv, char *argc[]){
std::map<string, int> MyMap;
std::istream_iterator<string> begin(cin);
std::istream_iterator<string> end;
while(begin != end){
string word = *begin;
transform(word.begin(), word.end(), word.begin(), ::tolower);
for (int i = 0, len = word.size(); i < len; i++){
if (ispunct(word[i])){
word.erase(i--, 1);
len = word.size();
}
}
if(*begin == string("\0"))
break;
MyMap[word]++;
*begin++;
}
multimap<int, string> reverseTest = flip_map(MyMap);
cout << "\nWord Count :D :\n" << endl;
for(multimap<int, string>::const_reverse_iterator it = reverseTest.rbegin(); it != reverseTest.rend(); ++it)
cout << it -> first << " " << it -> second << endl;
return 0;
}