PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 20, 2022

[FIXED] how to convert string to vector<int> c++

 July 20, 2022     c++, integer, string, vector     No comments   

Issue

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


Solution

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');
    }
}


Answered By - K.R.Park
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing