-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest.cpp
More file actions
72 lines (69 loc) · 2.12 KB
/
test.cpp
File metadata and controls
72 lines (69 loc) · 2.12 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
#include <set>
#include <vector>
#include <map>
#include <sstream>
#include <fstream>
#include <iostream>
#include <utility>
using namespace std;
int main(int argc, char *argv[])
{
// Sets
set <string> strset;
strset.insert("bug");
strset.insert("horse");
strset.insert("horse");
set<string>::iterator findit;
findit = strset.find("horse");
cout << "Found horse "<< *findit<<endl;
for (set<string>::iterator it=strset.begin(); it!=strset.end(); ++it)
cout << ' ' << *it;
cout << endl;
vector<string> tokens;
set <string> unique;
string next_line; // Each data line
ifstream in(argv[1]);
while (getline(in, next_line)) {
istringstream iss(next_line);
string token;
while (iss >> token) {
string nopunct = "";
for(auto &c : token) { // Remove Punctuation
if (isalpha(c)) {
nopunct +=c;
}
}
tokens.push_back(nopunct);
unique.insert(nopunct);
// cout << token<<endl;
}
}
cout << "Number of words "<<tokens.size()<<endl;
cout << "Number of unique words "<<unique.size()<<endl;
for (set<string>::iterator it=unique.begin(); it!=unique.end(); ++it)
cout << ' ' << *it;
cout << endl;
// pairs
pair <string,int> mypair;
mypair.first = "Hello";
mypair.second = 8;
cout << mypair.first<<" "<<mypair.second<<endl;
set <pair <string, int>> pairset;
pairset.insert(mypair);
set<pair <string, int>>::iterator findpair;
findpair = pairset.find(mypair);
cout << "found First "<<findpair->first<<" second "<<findpair->second<<endl;
// maps
map <string, int> wordcount;
wordcount["foo"] = 5;
cout << "wordcount for foo "<<wordcount["foo"]<<endl;
wordcount.insert(mypair);
cout << "wordcount for Hello "<<wordcount["Hello"]<<endl;
wordcount.clear();
for(auto s:tokens) {
wordcount[s]++;
}
for (map<string,int>::iterator it=wordcount.begin(); it!=wordcount.end(); ++it)
cout << it->first<<' ' << it->second<<endl;
cout << endl;
}