-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSplittableString.hpp
57 lines (42 loc) · 1.2 KB
/
SplittableString.hpp
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
#ifndef SPLITTABLE_STRING
#define SPLITTABLE_STRING
#include <string>
#include <vector>
#include <iostream>
class SplittableString : public std::string
{
public :
SplittableString() : std::string(){}
SplittableString(const std::string& str) : std::string(str){}
SplittableString(const char* str) : std::string(str){}
std::vector<SplittableString> split(const std::string& sequence) const
{
std::vector<SplittableString> result;
size_t i = 0;
size_t split_position;
while (i < size() && (split_position = find(sequence, i)) != std::string::npos)
{
result.push_back(substr(i, split_position - i));
i = split_position + sequence.length();
}
if (i < size())
{
result.push_back(substr(i));
}
return result;
}
};
// int main(void)
// {
// SplittableString str = "f 102/347/121 118/348/121 103/349/121";
// for (const SplittableString& s: str.split(" "))
// {
// for (const SplittableString& n : s.split("/"))
// {
// std::cout << n << "&";
// }
// std::cout << "\n" << std::endl;
// }
// return 0;
// }
#endif