Reputation: 517
I would like to read a string of digits then push the digits to a vector one by one.
string _inValue = "12345";
vector<int> _value;
void superint::setVector()
{
for(int i=0; i < _inValue.length(); ++i)
{
_value.push_back(_inValue[i]);
}
}
What I do is this: I will push_back the ASCII value of '1'
(49
). I want it to have the value 1
.
Is the best way to just use _inValue[i]-48
?
Upvotes: 1
Views: 192
Reputation: 56863
You need to subtract the value of the ASCII '0':
_value.push_back(_inValue[i]-'0');
That is much more descriptive than just using a magic number like 48.
Upvotes: 4