Connecting Tech Pros Worldwide Help | Site Map

Conversion from int and float to string / c++

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 04:51 AM
Kamil Grymuza
Guest
 
Posts: n/a
Default Conversion from int and float to string / c++

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

Thanks
Kamil Grymuza

  #2  
Old July 23rd, 2005, 04:51 AM
Mike Wahler
Guest
 
Posts: n/a
Default 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


  #3  
Old July 23rd, 2005, 04:51 AM
Victor Bazarov
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.