CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Sep 2003
    Posts
    815

    atoi in std:string

    How do I perform atoi on std:string

  2. #2
    Join Date
    Nov 1999
    Location
    Copenhagen, Denmark
    Posts
    265
    Did you try std::string::c_str() method:

    Code:
    #include <cstdlib>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main() 
    {
    	string s("23");
    
    	cout << atoi(s.c_str()) << endl;
    	
    	return 0;
    }
    Best regards,
    S. Bro

    "I would be happy to deal with my problems one at the time if they would only line up!"
    -Paul Cilwa, "Borland C++ Insider".

    Other useful fora some of which I ruthlessly clipboarded from other peoples footers.

    MSDN: http://search.microsoft.com/us/dev/default.asp
    WIN 32 Assembler: http://board.win32asmcommunity.net/
    RDBMS: http://www.dbforums.com
    Robert's Perl Tutorial: http://www.sthomas.net/roberts-perl-tutorial.htm
    Merriam-Webster Online: http://www.m-w.com/home.htm
    Writing Unmaintainable Code: http://mindprod.com/unmain.html

  3. #3
    Join Date
    Sep 2003
    Posts
    815
    is there a soloution without casting?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by avi123
    is there a soloution without casting?
    What casting?

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Sep 2003
    Posts
    815
    I mean without using c_str
    is this an 'expensive' function?

  6. #6
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    720
    In MS-STL it conditionaly returns pointer, in SGI -- just returns it.

  7. #7
    Join Date
    Sep 2003
    Posts
    815
    so it's not expensive, right?

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by avi123
    so it's not expensive, right?
    How many times are you expecting to use c_str()? I would argue that atoi() is an "expensive" function, and c_str() adds no overhead, but that depends on the implementaion of std::string.

    In any event, to get a const char * from a std::string, you must call c_str(), and this is what atoi() is expecting.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Sep 2003
    Posts
    815
    Paul

    I must use atoi or eqvivalent function
    I have std:string and not char *, if there is no way to do it directly on the std:string then I'll use c_cstr and then atoi,
    but I must perform atoi (or similar function)

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by avi123
    Paul

    I must use atoi or eqvivalent function
    I have std:string and not char *, if there is no way to do it directly on the std:string then I'll use c_cstr and then atoi,
    but I must perform atoi (or similar function)
    Your only choice is to use c_str(). The buffer for a std::string is not guaranteed to be contiguous, so getting a direct pointer to the buffer is not guaranteed to work. Also, the buffer for a std::string is more than likely a private member variable, therefore you don't have access to it.

    An implementation of a string class (even though it isn't called std::string) called the std::rope (http://www.sgi.com/tech/stl/Rope.html)
    that SGI STL supports, is an example where the characters for the string are not stored in a contiguous buffer.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Sep 2003
    Posts
    815
    so is it safe to use c_csr and then atoi?

  12. #12
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by avi123
    so is it safe to use c_csr and then atoi?
    Yes...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured