Thread: strtol() is the only way?

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    strtol() is the only way?

    I'm trying to grab a C++ style string and fill a vector<unsigned int> with the same size as the string has characters. Each character on the string is a number from 0 to 9. As such the vector will have ints ranging from 0 to 9.

    Follows a piece of the code with a literal string for testing purposes.

    Code:
    int strto_int (const string &str) {
        const char *pstr = str.c_str();
        char *pend;
        return static_cast<int>( strtol(pstr, &pend, 0) );
    }
    
    int main()
    {
        string frame = "123423544126673467372411345223672338990";
        vector<unsigned int> vframe;
    
        for (string::size_type i = 0; i != frame.size(); ++i)
            vframe.push_back( strto_int(frame.substr(i, 1)) );
    }
    My question relates to strtol(). I couldn't find any other way to convert. It seems to me though excessive to have to convert a C++-style string to a C-style string because I can only use C-style function to do some work on that C++ string. Is strtol() really the only way to convert? Or am I missing something on the STL?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Or am I missing something on the STL?
    Well, technically it's not a part of the STL. You're looking for std::stringstream. The FAQ has info on how to use it. Or if that's too verbose for you, Boost has a lexical_cast template that works nicely.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    One way
    Code:
        string frame = "123423544126673467372411345223672338990";
        vector<unsigned int> vframe(frame.length());
    
        for (string::size_type i = 0; i != frame.length(); ++i)
            vframe[i] = frame[i]-'0';
    Kurt

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by Prelude
    You're looking for std::stringstream.
    Ahh, streams. Still have to learn about those. Thanks Prelude.

    Quote Originally Posted by Kurt
    vframe[i] = frame[i]-'0';
    Loved it!
    But you really need to help me understand what you are doing in there
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Ascii codes. Got it!
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Mario F.
    But you really need to help me understand what you are doing in there
    Code:
        string frame = "123423544126673467372411345223672338990";
        // create vector of right size to be able to use operator []
        vector<unsigned int> vframe(frame.length());
    
        for (string::size_type i = 0; i != frame.length(); ++i)
            vframe[i] = frame[i]-'0';  
           //  the characters '0' .. '9' are in sequence for shure, so frame[i]-'0' works to convert to integer
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. explanation of STRTOL function
    By adash in forum C Programming
    Replies: 2
    Last Post: 05-25-2009, 02:54 PM
  2. strtol() / malloc
    By sniperwire in forum C Programming
    Replies: 3
    Last Post: 11-02-2008, 09:01 PM
  3. strtol
    By kiz in forum C Programming
    Replies: 9
    Last Post: 10-21-2007, 11:42 AM
  4. Parse line using strtol
    By mike_g in forum C++ Programming
    Replies: 3
    Last Post: 10-05-2007, 09:47 AM
  5. strtol with floating point
    By Laserve in forum C Programming
    Replies: 3
    Last Post: 07-26-2004, 06:02 AM