-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
60 lines (50 loc) · 1.44 KB
/
utils.cpp
File metadata and controls
60 lines (50 loc) · 1.44 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
#include "utils.h"
#include <regex>
#include <sstream>
#include <string>
using namespace std;
vector<int> parse_numbers(string line) {
vector<int> numbers;
stringstream ss(line);
int number;
while (ss >> number) {
numbers.push_back(number);
}
return numbers;
}
vector<int> parseNumbersDelimiter(string line, char delimiter) {
vector<int> parts;
stringstream ss(line);
string part;
while (getline(ss, part, delimiter)) {
parts.push_back(stoi(part));
}
return parts;
}
vector<long> extractAllNumbers(string line) {
vector<long> numbers;
for (int i = 0; i < line.size(); i++) {
if (isdigit(line[i]) ||
(line[i] == '-' && i != line.size() - 1 && isdigit(line[i + 1]))) {
int j = i;
while (j < line.size() && (isdigit(line[j]) || line[j] == '-')) {
j++;
}
numbers.push_back(stol(line.substr(i, j - i)));
i = j;
}
}
return numbers;
}
vector<string> splitString(const string line, string delimiter) {
vector<string> parts;
size_t pos_start = 0;
size_t pos_end, delim_len = delimiter.length();
while ((pos_end = line.find(delimiter, pos_start)) != string::npos) {
string token = line.substr(pos_start, pos_end - pos_start);
pos_start = pos_end + delim_len;
parts.push_back(token);
}
parts.push_back(line.substr(pos_start));
return parts;
}