How do I loop through a string consisting of numbers and letters and add only numbers to the vector?
For example if the input is:
e385p336J434Y26C2Z6X5Z2
I want to get a vector of int like this:
number = {385, 336, 434, 26, 2, 6, 5, 2}
The best I got was to iterate over the line and add all the digits like that:
#include <bits/stdc++.h>
int main(){
string f = "e385p336J434Y26C2Z6X5Z2";
vector<int> f_numb;
string sum;
for (int i = 0; i < f.size(); ++i){
if (('0' <= f[i]) && (f[i] <= '9')){
sum += (f[i]);
}
}
//std::cout << sum << std::endl;
vector<int> m_numb;
for (int i = 0; i < sum.size(); ++i){
m_numb.push_back(sum[i] - '0');
}
int sm;
for (int i = 0; i < m_numb.size(); ++i){
sm += m_numb[i];
std::cout << m_numb[i] << " ";
}
std::cout << std::endl;
}