Connecting Tech Pros Worldwide Forums | Help | Site Map

Conversion from int and float to string / c++

Kamil Grymuza
Guest
 
Posts: n/a
#1: Jul 23 '05
Hello
How to convert int and float values to strings? I mean std::string not
char*.

Thanks
Kamil Grymuza

Mike Wahler
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Conversion from int and float to string / c++



"Kamil Grymuza" <grymuz@wp.pl> wrote in message
news:d74r1j$pg0$1@nemesis.news.tpi.pl...[color=blue]
> Hello
> How to convert int and float values to strings? I mean std::string not
> char*.[/color]

#include <iostream>
#include <sstream>
#include <stream>

template <typename T>
std::string to_string(const T& value)
{
std::ostringstream oss;
oss << value;
return oss.str();
}

int main()
{
std::string s1(to_string(42));
std::string s2(to_string(3.14f));

std::cout << s1 << '\n'
<< s2 << '\n';

return 0;
}

-Mike


Victor Bazarov
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Conversion from int and float to string / c++


Kamil Grymuza wrote:[color=blue]
> How to convert int and float values to strings? I mean std::string not
> char*.[/color]

See the FAQ or the archives. Alternatively, read about ostringstream
class.

V
Closed Thread