Last active
August 6, 2022 17:56
-
-
Save dev-0x7C6/6683e75f2c51af3ff6accc0c7d75ba99 to your computer and use it in GitHub Desktop.
C++: Split string_view into std::vector<string_view> by delimiter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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