Created
March 3, 2021 23:59
-
-
Save sergiosvieira/8066d6afd7565712e5a583d752953bec to your computer and use it in GitHub Desktop.
split
This file contains 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 <vector> | |
using Vector = std::vector<std::string>; | |
Vector split(const std::string& input, const std::string& delimiter) { | |
size_t b = 0, e = 0; | |
Vector result; | |
while ((e = input.find_first_of(delimiter, b)) <= std::string::npos) { | |
result.emplace_back(input.substr(b, e - b)); | |
if (e == std::string::npos) break; | |
b = e + 1; | |
} | |
return result; | |
} | |
int main() { | |
Vector v = split("192.168.1.2", "."); | |
for (const auto& i: v) { | |
std::cout << i << '\n'; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment