strings <---> numbers

  • strings ---> numbers
     1 #include "stdafx.h"
    2 #include <iostream>
    3 #include <sstream>
    4 using namespace std;
    5
    6 int _tmain(int argc, _TCHAR* argv[])
    7 {
    8 stringstream os;
    9 os << "12345 67.89"; // insert a string of numbers into the stream
    10 int nValue;
    11 double dValue;
    12
    13 os >> nValue >> dValue;
    14
    15 cout << nValue << " " << dValue << endl;
    16
    17 return 0;
    18 }
  • numbers ---> strings
     1 #include "stdafx.h"
    2 #include <iostream>
    3 #include <sstream>
    4 using namespace std;
    5
    6 int _tmain(int argc, _TCHAR* argv[])
    7 {
    8 stringstream os;
    9
    10 int nValue = 12345;
    11 double dValue = 67.89;
    12 os << nValue << " " << dValue;
    13
    14 string strValue1, strValue2;
    15 os >> strValue1 >> strValue2;
    16
    17 cout << strValue1 << " " << strValue2 << endl;
    18
    19 return 0;
    20 }
原文地址:https://www.cnblogs.com/dahai/p/2277938.html