-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.h
executable file
·64 lines (53 loc) · 1.76 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
57
58
59
60
61
62
63
64
#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(std::set<T>& s1, std::set<T>& s2)
{
typename std::set<T>::iterator it1 = s1.begin();
typename std::set<T>::iterator it2 = s2.begin();
typename std::set<T> returnThis;
//increments one at a time to find the intersections
//if there is an intersection both increment
while(it1 != s1.end() and it2 != s2.end()){
if(*it1 == *it2){
returnThis.insert(*it1);
++it1;
++it2;
}else if(*it1 < *it2){
++it1;
}else{
++it2;
}
}
return returnThis;
}
template <typename T>
std::set<T> setUnion(std::set<T>& s1, std::set<T>& s2)
{
//just adds both s1 and s2 into the same set to be returned
//takes care of duplicates automatically
typename std::set<T> returnThis;
returnThis.insert(s1.begin(), s1.end());
returnThis.insert(s2.begin(), s2.end());
return returnThis;
}
/***********************************************/
/* 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