-
Notifications
You must be signed in to change notification settings - Fork 0
/
String.h
58 lines (47 loc) · 1.61 KB
/
String.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
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
std::string ToString(int i);
//std::string ToString(int64_t i);
int ToInt(std::string s);
static std::ostream& operator<< (std::ostream& os, const std::vector<int>& v)
{
for (auto i : v)
os << i << " ";
return os;
}
// Splits a string based on a separator string (not the characters inside the separator string)
// separators at the start or end of the string are ignored
static void Split(std::vector<std::string> &tokens, const std::string &s, const std::string &separator)
{
if (s.size() == 0) throw std::runtime_error("no string passed in");
if (separator.size() == 0) throw std::runtime_error("no separator passed in");
tokens.clear();
size_t prev = 0;
size_t pos = s.find(separator);
while (pos != std::string::npos)
{
if (pos != 0) // if we found a separator at the beginning, ignore it
tokens.push_back(s.substr(prev, pos - prev));
prev = pos + separator.size();
pos = s.find(separator, prev);
}
std::string last = s.substr(prev);
if (last.size())
tokens.push_back(last);
}
static std::vector<std::string> GetLines(const std::string &fileName)
{
std::ifstream inFile;
inFile.open(std::string("problems/data/") + fileName);
if (!(inFile && inFile.is_open())) throw std::runtime_error("could not open file");
std::vector<std::string> lines;
std::string line;
while (!inFile.eof())
{
getline(inFile, line);
lines.push_back(line);
}
return lines;
}