Created
February 28, 2018 10:15
-
-
Save niklas88/eea4a2e850c937f6a6b66d358f4a60a1 to your computer and use it in GitHub Desktop.
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
#include <iostream> | |
#include <string> | |
#include <string_view> | |
#include <vector> | |
std::vector<std::string_view> split_no_strcpy(std::string_view orig, const char sep) { | |
std::vector<std::string_view> result; | |
if (orig.size() > 0) { | |
size_t from = 0; | |
size_t sepIndex = orig.find(sep); | |
while (sepIndex != std::string::npos) { | |
result.emplace_back(&orig[from], sepIndex - from); | |
from = sepIndex + 1; | |
sepIndex = orig.find(sep, from); | |
} | |
result.emplace_back(&orig[from], orig.size() - from); | |
} | |
return result; | |
} | |
int main(int argc, char **argv) { | |
std::string hello = u8"Hello World what up?"; | |
auto splits = split_no_strcpy(hello, ' '); | |
for (std::string_view split : splits) { | |
std::cout << split << std::endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment