-
Notifications
You must be signed in to change notification settings - Fork 0
/
StrOp.cpp
49 lines (43 loc) · 1.51 KB
/
StrOp.cpp
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
//
// Created by Kyle Bolton on 10/18/16.
//
#include "StrOp.h"
void StrOp::split(const char& delim, const std::string& string_to_split, std::vector<std::string>& elements) {
std::stringstream ss;
ss.str(string_to_split);
std::string item;
while (std::getline(ss, item, delim)) {
elements.push_back(item);
}
}
std::vector<std::string> StrOp::split(const char& delim, const std::string& string_to_split) {
std::vector<std::string> elements;
StrOp::split(delim, string_to_split, elements);
return elements;
}
template<typename T>
std::string StrOp::join(const std::string& delim, std::vector<T>& elements){
std::vector<std::string> stringified_elements;
for(const T& element: elements){
stringified_elements.push_back(std::to_string(element));
}
return join(delim, stringified_elements);
}
template<>
std::string StrOp::join<std::string>(const std::string& delim, std::vector<std::string>& elements){
std::ostringstream stream;
std::copy(elements.begin(), elements.end(), std::ostream_iterator<std::string>(stream, delim.c_str()));
stream << elements.back();
std::string joined_string = stream.str();
joined_string.pop_back();
joined_string.pop_back();
return joined_string;
}
template<>
std::string StrOp::join<unsigned int>(const std::string& delim, std::vector<unsigned int>& elements){
std::vector<std::string> stringified_elements;
for(const unsigned int& element: elements){
stringified_elements.push_back(std::to_string(element));
}
return join(delim, stringified_elements);
}