프로그래밍/코드 조각
c++ 문자열 스플릿
제페
2018. 5. 10. 19:59
반응형
#include <string>
#include <vector>
#include <regex>
std::vector<std::string> SplitString(const std::string& source, const std::string& spliters)
{
const std::regex r{ spliters };
std::sregex_token_iterator begin{ source.begin(), source.end(), r, -1 };
std::sregex_token_iterator end{};
std::vector<std::string> tokens;
tokens.reserve(std::distance(begin, end));
for (auto i = begin; i != end; ++i)
{
tokens.push_back(*i);
}
return tokens;
}
샘플 코드
int main()
{
std::string str = "cat dog melon";
auto strs = SplitString(str, " ");
for(const auto& str : strs)
{
std::cout << str << std::endl;
}
return 0;
}
output >>
cat
dog
melon
반응형