-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringutils.h
59 lines (49 loc) · 1.17 KB
/
stringutils.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
// implement the basic parsing utils that are commonly used in python
// split, join, strip, etc
#ifndef _STRINGUTILS_H_
#define _STRINGUTILS_H_
#include<vector>
#include<string>
using namespace std;
string strip(string s, char delim = ' ') {
int start = 0;
int end = s.size() - 1;
while (start < s.size() && s[start] == delim) {
start++;
}
while (end >= 0 && s[end] == delim) {
end--;
}
return s.substr(start, end - start + 1);
}
vector<string> split(const string& s, char delim) {
vector<string> tokens;
stringstream ss(s);
string token;
while (getline(ss, token, delim)) {
tokens.push_back(strip(token, ' '));
}
return tokens;
}
string join(vector<string>& tokens, char delim) {
string s;
for (int i = 0; i < tokens.size(); i++) {
s += tokens[i];
if (i != tokens.size() - 1) {
s += delim;
}
}
return s;
}
template <typename T>
vector<T> splitT(string& s) {
vector<T> tokens;
stringstream ss(s);
cin.rdbuf(ss.rdbuf());
T token;
while (cin >> token) {
tokens.push_back(token);
}
return tokens;
}
#endif // _STRINGUTILS_H_