Thread: converting ints to strings?

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    Lightbulb converting ints to strings?

    What is the completely easiest way to convert an int to a string? I have heard of classes such as stringstream but i am wondering if there is another approach ? (i assume theres nothing like in java, with an integer wrapper class?)

    for example what if i wanted to save the string "0" to my array slot 0, and "1" to my array slot 1?

    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
    	int sz;
    	cin >> sz;
    	string *arr = new string[sz];
            //for loop that converts its index to string SOMEHOW(?), and saves it to arr[i]
            for(int i =0 ; i < sz ; ++i)
            {
                  arr[i] = i;  //Wrong
                  cout << arr[i];
            }
    }
    thanks!


    edit: also thought about that since a string is actually a char pointer array(from what i hear!) that I can just cast a char to an int? is that way?
    Last edited by rodrigorules; 11-25-2009 at 03:53 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rodrigorules
    What is the completely easiest way to convert an int to a string? I have heard of classes such as stringstream but i am wondering if there is another approach ?
    If I had to rely solely on the standard library, I would indeed use a std::stringstream, e.g.,
    Code:
    #include <vector>
    #include <string>
    #include <sstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int sz;
        cin >> sz;
        vector<string> arr(sz);
    
        // for loop that converts its index to string and saves it to arr[i]
        for (vector<string>::size_type i = 0; i < arr.size(); ++i)
        {
            stringstream ss;
            ss << i;
            ss >> arr[i];
        }
    
        for (vector<string>::size_type i = 0; i < arr.size(); ++i)
        {
            std::cout << arr[i] << '\n';
        }
        std::cout << std::endl;
    }
    Notice that instead of dynamically allocating an array and then forgetting to delete[] it, I used a std::vector. This is not Java, so gratuitous use of new and new[] should be avoided.

    If I had access to Boost, I might consider using boost::lexical_cast instead (but that actually uses stringstreams to do the job).

    Quote Originally Posted by rodrigorules
    edit: also thought about that since a string is actually a char pointer array(from what i hear!) that I can just cast a char to an int? is that way?
    C-style strings are contiguous sequences of char terminated by a null character, but here you are using std::string objects. Speaking of which, I changed the header included from <cstring> to <string>. Anyway, you cannot just cast a char to int, since that will give you the value of the char, not the int corresponding to the digit.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    There's boost::lexical_cast which allows you to do

    Code:
    int n = 100;
    std::string s = boost::lexical_cast<std::string>(100);
    However, conversion to string is simpler than the other way round (less chances of failure), so it wouldn't be hard to implement a to_string function for any type supporting operator<< using only standard libraries:

    Code:
    #include <sstream>
    #include <string>
    
    template <class T>
    std::string to_string(const T& value)
    {
        std::stringstream ss;
        ss << value;
        return ss.str();
    }
    
    int main()
    {
         int n = 100;
         std::string s = to_string(n);
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to handle ints and strings when writing to file
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 04-23-2008, 04:44 AM
  2. converting c style strings to c++ strings
    By fbplayr78 in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 03:13 AM
  3. Strings into Ints
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2002, 04:07 PM
  4. converting strings to ints
    By HomerJ in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2002, 06:08 PM
  5. Converting strings to ints
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2002, 02:48 PM