string to int array ?

This is a discussion on string to int array ? within the C++ Programming forums, part of the General Programming Boards category; I've searched around and can't find out how to convert a string into an int array. so that: Code: string ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    9

    Question string to int array ?

    I've searched around and can't find out how to convert a string into an int array.

    so that:
    Code:
    string myString = "12345" ;
    turns into:
    Code:
    int x[ 5 ] = { 1, 2, 3, 4, 5 } ;

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    might be easier to turn it into a vector

    but look into copy algorithm in STL, or memcpy from C

    not sure if this will compile... but here maybe

    Code:
    int *x = new int[myString.length()];
    copy(myString.begin(), myString.end(), x);
    for(int i=0;i<myString.length();++i)
    x[i] = x[i]-'0';
    Last edited by rodrigorules; 05-08-2010 at 10:16 PM.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    38
    There is a post about a reverse string, same principal. I got it to work with a for loop and string.length().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 06:26 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21