What is the easies way to convert int to std::string?

This is a discussion on What is the easies way to convert int to std::string? within the C++ Programming forums, part of the General Programming Boards category; int x, how to convert x to std::sting type?...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    What is the easies way to convert int to std::string?

    int x,
    how to convert x to std::sting type?

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Code:
    #include <sstream>
    
    std::stringstream int_to_string;
    
    int x = 0;
    
    int_to_string << x;
    
    std::cout << int_to_string.str() << std::endl;
    thats the simplest way I know.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    #include <sstream>

    std::stringstream int_to_string;
    int x = 0;
    int_to_string << x;
    std::string str(int_to_string.str());

    Correct?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    youre creating a new string from the string returned by the .str(). seems it should work. best way to find out is to try it

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    Alternatively:
    Code:
    template <class var_t>
    std::string ToString(var_t data)
    {
        static std::stringstream ss;
        ss << data;
        return ss.str();
    }

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If you have boost. Use lexical_cast. It basically does the same thing under the hood, but it provides a uniform interface for all of these types of conversions.

    I'm not sure if that's in std::tr1 or not, but it might be.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by Desolation View Post
    Alternatively:
    Code:
    template <class var_t>
    std::string ToString(var_t data)
    {
        static std::stringstream ss;
        ss << data;
        return ss.str();
    }
    Why is that stream static? That introduces at least two bugs right there.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 07:19 AM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM

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