-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA1final.cpp
More file actions
81 lines (73 loc) · 1.5 KB
/
Copy pathA1final.cpp
File metadata and controls
81 lines (73 loc) · 1.5 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* 60-340 CPP
Assignment 1
Prabhjit Singh
-A program takes in users input of words,
and shows how many times the word is inputted in an unsorted
histogram with the following format:
An unsorted frequency histogram of the input is:
bear |****
cat |**
dog |*
house |***
tiger |**
*/
#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
int main()
{
std::map<std::string, unsigned> hist;
string s;
while (cin >> s)
++hist[s];
//using a built in algorithim
//using iterators, takes 2 iterators (begingin to end)
//3rd argument is a function
//that takes whatever you are visiting
//in this case its the string
//in historgram
auto const longest_entry = std::max_element
(
begin(hist),
end(hist),
[](auto const& elem1, auto const& elem2)
{
return elem1.first.size() < elem2.first.size();
}
);
int longest_word_length;
longest_entry == end(hist) ?
longest_word_length = 0
:
longest_word_length = longest_entry->first.size();
cout << '\n' << '\n'
<< "An unsorted frequency histogram of the input is:"
<< '\n';
/*
using std::for_each to get the output as such:
An unsorted frequency histogram of the input is:
bear |****
cat |**
dog |*
house |***
tiger |**/
for_each(
begin(hist), end(hist),
[longest_word_length](auto const& elem)
{
cout << left
<< setfill(' ')
<< setw(longest_word_length)
<< elem.first
<< " |"
<< setfill('*')
<< setw(elem.second)
<< ""
<< '\n';
}
);
return 0;
}