Int and parsing strings
4 answers
Charles is a very straightforward path. However, it's not hard to convert the number to a string and do some string processing if we don't want to wrestle with the math :)
Here is the procedure we want to do:
306 -> "306" -> ['3' ,'0', '6'] -> [3,0,6]
Some language is very easy to do this (Ruby):
>> 306.to_s.split("").map {|c| c.to_i}
=> [3,0,6]
Some need more work, but still very clear (C ++):
#include <sstream>
#include <iostream>
#include <algorithm>
#include <vector>
int to_digital(int c)
{
return c - '0';
}
void test_string_stream()
{
int a = 306;
stringstream ss;
ss << a;
string s = ss.str();
vector<int> digitals(s.size());
transform(s.begin(),s.end(),digitals.begin(),to_digital);
}
+1
source to share