I have a string of non-uniform space separated integers ,I want to do some arithmetic operations on the elements so I have decided to first convert the string to integer array. Below is my approach:
string s; //let s="1 2 30 54 899 2 7 3 1";
cin>>s;
int n=s.length();
vector<int>arr(n);
for(int i=0;i<n;i++)
{
if(s[i]==' ')continue;
else{
arr.push_back(s[i]-'0');
}
}
for(int i=0;i<n;i++)
{
cout<<arr[i]<<endl; // arr should be{1,2,30,54,899,2,7,3,1};
}
What is wrong in this approach ?