Skip to content

Commit

Permalink
convert vec of strings to vec of ints
Browse files Browse the repository at this point in the history
  • Loading branch information
petacreepers23 committed Dec 2, 2024
1 parent b419f5f commit 42317a0
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/utilityString.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <string>
#include <vector>
#include <charconv>

/*
This file will only have things related with any kind of manipulation to std::string
Expand Down Expand Up @@ -28,4 +29,19 @@ namespace P23 {
return ret;
}

/*
Converts a vector of strings which contains numbers to a vector of numbers.
Ejemplo: vec = {"1", "2", "3", "4"}
from_string_vec_to_int_vec(vec) -> {1, 2, 3, 4}
*/
std::vector<int> from_string_vec_to_int_vec(std::vector<std::string> vec) {
std::vector<int> ret;
for (const auto& str : vec) {
int num;
std::from_chars(str.data(), str.data() + str.size(), num);
ret.push_back(num);
}
return ret;
}
}

0 comments on commit 42317a0

Please sign in to comment.