-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusealgo.cpp
More file actions
66 lines (54 loc) · 1.59 KB
/
Copy pathusealgo.cpp
File metadata and controls
66 lines (54 loc) · 1.59 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
//
// Created by 赵健 on 2021/8/17.
//
#include "usealgo.h"
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <iterator>
#include <algorithm>
#include <cctype>
using namespace std;
char toLower(char ch) { return tolower(ch); }
string &ToLower(string &st);
void display(const string &s);
//The dog saw the cat and thought the cat fat
//The cat thought the cat perfect
//quit
void usealgo::test() {
vector<string> words;
cout << "Enter words (enter quit to quit):\n";
string input;
while(cin >> input && input != "quit") {
words.push_back(input);
}
cout << "You entered the following words:\n";
for_each(words.begin(), words.end(), display);
cout << endl;
// place words in set, converting to lowercase
set<string> wordset;
transform(words.begin(), words.end(), insert_iterator<set<string>>(wordset, wordset.begin()), ToLower);
cout << "\nAlphabetic list of wordls:\n";
for_each(wordset.begin(), wordset.end(), display);
cout << endl;
// place word and frequency in map
map<string, int> wordmap;
set<string>::iterator si;
for (si = wordset.begin(); si != wordset.end(); si++){
wordmap[*si] = count(words.begin(), words.end(), *si);
}
// display map contents
cout << "\nWord frequency:\n";
for(si = wordset.begin(); si != wordset.end(); si++) {
cout << *si << ": " << wordmap[*si] << endl;
}
}
string &ToLower(string &st) {
transform(st.begin(), st.end(), st.begin(), toLower);
return st;
}
void display(const string &s) {
cout << s << " ";
}