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

    Convert int to String and vice versa

    Hi

    Can any one let me know how to convert int to string and vice versa.

    Thanks


    ----------------------------------------------------------
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>

    int main()
    {
    vector<string> removeClusters;

    int i = 0;
    int j = 2;


    //FALLS HERE
    removeClusters.push_back(i+j);
    removeClusters.push_back("1");


    for (int i = 0; i < (removeClusters.size() ) ; i++)
    {
    cout << "Cluster Vector Data " << removeClusters[i] << endl;
    }

    return 0;
    }

  2. #2
    Join Date
    Apr 2000
    Location
    Frederick, Maryland
    Posts
    507
    Try this code

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <sstream>
    
    int main()
    {
    	vector<string> removeClusters;
    
    	int i = 0;
    	int j = 2;
    
    	ostringstream os;
    	os << i+j;
    
    	removeClusters.push_back(os.str());
    	removeClusters.push_back("1");
    
    	for (int k = 0; k < (removeClusters.size() ) ; k++)
    	{
    		cout << "Cluster Vector Data " << removeClusters[k] << endl;
    	}
    
    	return 0;
    }
    Hope it helps.

  3. #3
    Join Date
    Jan 2003
    Posts
    15
    This code doesn't work.

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    [list][*]add : using namespace std ... to previous code
    [*]Paul McKenzie posted a function to do this earlier,
    but I could not find it (and the codeguru repsonse time
    seems to be very slow lately). Here is one you can
    try. I added an extra argument for setting the precision
    if desired.

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <sstream>
    #include <iomanip>
    
    using namespace std;
    
    template <typename T>
    std::string number2string(T value, int precision=0)
    {
        ostringstream os;
        if (precision != 0) os << std::setprecision(precision);
        os << value;
        return os.str();
    }
    
    int main()
    {
        vector<string> removeClusters;
    
        int i = 0;
        int j = 2;
    
        removeClusters.push_back(number2string(i+j));
        removeClusters.push_back("1");
        removeClusters.push_back(number2string(5));
        removeClusters.push_back(number2string(6));
        removeClusters.push_back(number2string(3.141592654));
        removeClusters.push_back(number2string(3.141592654,10));
        removeClusters.push_back("1566");
    
        for (int k = 0; k < (removeClusters.size() ) ; k++)
        {
            cout << "Cluster Vector Data " << removeClusters[k] << endl;
        }
    
        return 0;
    }

  5. #5
    Join Date
    Jan 2003
    Posts
    15
    This code doesn't compiles.
    It comes up with error message

    sstream : No such file or directory.

  6. #6
    Join Date
    Aug 1999
    Posts
    586

    Re: Convert int to String and vice versa

    Originally posted by Sarika73
    Hi

    Can any one let me know how to convert int to string and vice versa.
    Check out "lexical_cast" at www.boost.org. This does things in the canonical C++ way. It may even become part of the next release of the standard itself (since the site is run by members of the C++ standards committee and the code there is targetted as as candidates for the next C++ release). Herb Sutter himself (secretary of the ISO and C++ standards committe) has confirmed this.

    E.g.,

    Code:
    #include <string>
    #include <boost/lexical_cast.hpp>
    using namespace std;
    using namespace boost;
    
    int main()
    {
         string NinetyAsString = lexical_cast<string>(90);
         int NinetyAsInteger = lexical_cast<int>(NinetyAsString);
    
         return 0;
    }
    Also check out the following useful class when you want to format a string on the fly. Note that "tstring" and "tostringstream" are defined for the Windows environment as:

    typedef std::basic_string<TCHAR> tstring;
    typedef std::basic_ostringstream<TCHAR> tostringstream;

    But you can change things back to accomodate the standard itself if you wish:

    Code:
    /////////////////////////////////////////////////////////////////////////////
    // "FormatStr" class. Use this class to format a "tstring" on the fly with
    // streaming arguments. It's basically the C++ answer to "sprintf()" style
    // argument lists. Instead of passing a format string and variable number
    // of arguments however, just construct a temporary as seen in the examples
    // below and stream the same arguments you would normally pass to any
    // "std::ostream" object via the "<<" operator. The result can then be
    // implicitly converted to a "tstring" via the "tstring()" operator so
    // you can effectively use it wherever you would use a "tstring" (frequently
    // as a "const tstring &" argument for instance - see example 2 below).
    // Also see "boost::lexical_cast" when you want to convert a single value
    // back to a string (where the emphasis is on conversion though both routines
    // will work - see "lexical_cast" for further details).
    //
    // Example 1
    // ---------
    //
    //   // Results in "Bill Gates is worth 50 billion dollars"
    //   tstring String = FormatStr() << _T("Bill Gates is worth ") << 50 << _T(" billion dollars");
    //
    // Example 2 (as above but pass to a function on the fly)
    // ---------
    //
    //   void SomeFunc(const tstring &String)
    //   {
    //       // ...
    //   }
    //
    //   SomeFunc(FormatStr() << _T("Bill Gates is worth ") << 50 << _T(" billion dollars");
    //
    /////////////////////////////////////////////////////////////////////////////
    class FormatStr
    {
    public:
        template <typename T>
        FormatStr &operator<<(const T &Whatever)
        {
            m_Stream << Whatever;
            return *this;
        }
    
        operator tstring() const
        {
            return m_Stream.str();
        }
    
    private:
        tostringstream m_Stream;
    };

  7. #7
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    you can use the c library function aoti and itoa.

  8. #8
    Join Date
    Jan 2003
    Posts
    15
    can you please give an example

  9. #9
    Join Date
    Aug 1999
    Posts
    586
    Originally posted by Sarika73
    can you please give an example
    See my post (with examples). That's the way you should normally do things.

  10. #10
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    sstream : No such file or directory
    You must have an old compiler. You can use the pre-standard
    strstream class. This class is still in the standard, but is
    deprecated. Modify my previus code as shown below.

    (also note for the suggestion to use itoa ... itoa is not
    standard in C++ , or C90 for that matter)

    Code:
    //#include <sstream>
    #include <strstream>
    #include <iomanip>
    
    using namespace std;
    
    template <typename T>
    std::string number2string(T value, int precision=0)
    {
    //    std::ostringstream os;
        std::ostrstream os;
        if (precision != 0) os << std::setprecision(precision);
        os << value << std::ends;
        string str(os.str());
        return str;
    }

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