What's Here?
- Members: 137,220
- Replies: 481,459
- Topics: 75,056
- Snippets: 2,567
- Tutorials: 675
- Total Online: 2,230
- Members: 105
- Guests: 2,125
|
Converts between types using string stream
|
Submitted By: jwwicks
|
|
|
Rating:
|
|
Views: 196 |
Language: C++
|
|
Last Modified: August 5, 2008 |
Snippet
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
using namespace std;
template <class oType, class iType>
oType ioConv( const iType& _T )
{
stringstream _S;
oType ret_val;
_S << _T;
_S >> ret_val;
return ret_val;
}
int main( int argc, char* argv[] )
{
char buffer[3][20] = { "1", "23.45", "Hello World..." };
int i =0;
double d = 0.0;
string s;
string s2;
i = ioConv<int>(buffer[0]);
d = ioConv<double>(buffer[1]);
s = ioConv<string>(buffer[2]);
s2 = ioConv<string>(d);
cout << "I: " << i << endl;
cout << "D: " << d << endl;
cout << "S: " << s << endl;
cout << "S2: " << s2 << endl;
return 0;
}
Copy & Paste
|
|
|
Reference Sheets
Bye Bye Ads
Monthly Drawing
Top Contributors
Top 10 Kudos This Month
|