Home > Enterprise >  how to convert string to vector<int> c
how to convert string to vector<int> c

Time:02-14

I would like to convert an string into a vector, so that it looks like the following:

string number = "0110";
vector < int > Vec; 

with the result:

Vec[0] = 0 
Vec[1] = 1
Vec[2] = 1
Vec[3] = 0

My problem is that the number starts with a 0, so using % doesn't seem to work if i first transform my string to an int

CodePudding user response:

I noticed that the question is modified to make it answerable:

#include <string>
#include <vector>

int main(){
    using namespace std;
    string number = "0110";
    vector < int > Vec;
    for(char& digit : number){
        Vec.push_back(digit - '0');
    }
}
  • Related