Skip to content

Instantly share code, notes, and snippets.

@dev-0x7C6
Last active August 6, 2022 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dev-0x7C6/6683e75f2c51af3ff6accc0c7d75ba99 to your computer and use it in GitHub Desktop.
Save dev-0x7C6/6683e75f2c51af3ff6accc0c7d75ba99 to your computer and use it in GitHub Desktop.
C++: Split string_view into std::vector<string_view> by delimiter.
// should work correctly, but not tested yet
std::vector<std::string_view> split(const std::string_view str, const char delimiter = ',')
{
std::vector<std::string_view> result;
auto mark = str.begin();
for (auto it = mark; it != str.end(); ++it) {
if (*it == delimiter) {
result.emplace_back(std::string_view(mark, it));
mark = it + 1;
}
}
if (std::distance(mark, str.end()))
result.emplace_back(std::string_view(mark, str.end()));
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment