-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.h
56 lines (47 loc) · 1.52 KB
/
util.h
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
#ifndef UTIL_H
#define UTIL_H
#include <string>
#include <iostream>
#include <set>
/** Complete the setIntersection and setUnion functions below
* in this header file (since they are templates).
* Both functions should run in time O(n*log(n)) and not O(n^2)
*/
template <typename T>
std::set<T> setIntersection(const std::set<T>& s1, std::set<T>& s2)
{
typename std::set <T> shared;
//returns same values in two sets as a set
for(typename std::set <T>:: iterator it =s1.begin(); it != s1.end(); ++it){
if(s2.find(*it)!=s2.end()){
shared.insert(*it);
}
}
return shared;
}
template <typename T>
std::set<T> setUnion(const std::set<T>& s1, std::set<T>& s2)
{
//returns a set of all words in sets
std::set <T> allwords;
for(typename std::set <T>:: iterator it1 =s1.begin(); it1 !=s1.end(); ++it1){
allwords.insert(*it1);
}
for(typename std::set <T>:: iterator it2 =s2.begin(); it2 !=s2.end(); ++it2){
allwords.insert(*it2);
}
return allwords;
}
/***********************************************/
/* Prototypes of functions defined in util.cpp */
/***********************************************/
std::string convToLower(std::string src);
std::set<std::string> parseStringToWords(std::string line);
// Used from http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
// Removes any leading whitespace
std::string <rim(std::string &s) ;
// Removes any trailing whitespace
std::string &rtrim(std::string &s) ;
// Removes leading and trailing whitespace
std::string &trim(std::string &s) ;
#endif