Click to See Complete Forum and Search --> : Convert int to String and vice versa


Sarika73
January 30th, 2003, 04:59 AM
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;
}

Zeeshan
January 30th, 2003, 05:46 AM
Try this 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.

Sarika73
January 30th, 2003, 08:44 AM
This code doesn't work.

Philip Nicoletti
January 30th, 2003, 09:47 AM
[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.


#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;
}

Sarika73
January 30th, 2003, 10:14 AM
This code doesn't compiles.
It comes up with error message

sstream : No such file or directory.

Sef
January 30th, 2003, 10:27 AM
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.,

#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:

/////////////////////////////////////////////////////////////////////////////
// "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;
};

mwilliamson
January 30th, 2003, 10:29 AM
you can use the c library function aoti and itoa.

Sarika73
January 30th, 2003, 10:46 AM
can you please give an example

Sef
January 30th, 2003, 10:51 AM
Originally posted by Sarika73
can you please give an example See my post (with examples). That's the way you should normally do things.

Philip Nicoletti
January 30th, 2003, 11:02 AM
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)


//#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;
}