C++_split
#C++ #文字列
他言語であるような文字列を任意の区切り文字で分割するsplitは/icons/C++.iconのSTLでは用意されてない
ので自前で実装する必要がある
find_first_ofを使うと早くて良いらしい
引用元:https://www.sejuku.net/blog/49378
code:split.cpp
vector<string> split(string str, char del) {
int first = 0;
int last = str.find_first_of(del);
vector<string> result;
while (first < str.size()) {
result.push_back(str.substr(first, last - first));
first = last + 1;
last = str.find_first_of(del, first);
if (last == string::npos) last = str.size();
}
return result;
}
int main() {
string str = "samurai,engineer,programmer,se";
char del = ',';
for (const auto subStr : split(str, del)) std::cout << subStr << '\n';
return 0;
}