Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JoshuaJakowlew/9ec89c9643f3818e2730c92ec1af71b4 to your computer and use it in GitHub Desktop.
Save JoshuaJakowlew/9ec89c9643f3818e2730c92ec1af71b4 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <string>
#include <string_view>
#include <algorithm>
auto find_colons(std::string_view s) -> std::vector<std::size_t>
{
std::vector<std::size_t> indices;
auto it = s.begin();
while ((it = std::find(it, s.end(), ':')) != s.end())
{
indices.emplace_back(std::distance(s.begin(), it++));
}
return indices;
}
auto find_digits(std::string_view s, const std::vector<std::size_t>& colon_indices) -> std::vector<std::string>
{
std::vector<std::string> numbers;
for (auto index : colon_indices)
{
index += 1;
const auto start = s.begin() + index;
if (const auto length = std::distance(start, std::find_if_not(start, s.end(), std::isdigit));
length)
{
numbers.emplace_back(s.substr(index, length));
}
}
return numbers;
}
int main()
{
std::string s = "hello:123world:1488:!";
for (auto i : find_digits(s, find_colons(s)))
std::cout << i << '\n';
std::cout << s << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment