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;
}
Re: Convert int to String and vice versa
Quote:
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;
};