Skip to content

Instantly share code, notes, and snippets.

@sergiosvieira
Created March 3, 2021 23:59
Show Gist options
  • Save sergiosvieira/8066d6afd7565712e5a583d752953bec to your computer and use it in GitHub Desktop.
Save sergiosvieira/8066d6afd7565712e5a583d752953bec to your computer and use it in GitHub Desktop.
split
#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