Conversion from int and float to string / c++

K

Kamil Grymuza

Hello
How to convert int and float values to strings? I mean std::string not
char*.

Thanks
Kamil Grymuza
 
M

Mike Wahler

Kamil Grymuza said:
Hello
How to convert int and float values to strings? I mean std::string not
char*.

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

template <typename T>
std::string to_string(const T& value)
{
std::eek:stringstream 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
 
V

Victor Bazarov

Kamil said:
How to convert int and float values to strings? I mean std::string not
char*.

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

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top