使用std::stringstream对字符串进行转换

要包含头文件#include

#include
#include
#include
using namespace std;

int main()
{
   cout << "Enter an integer: ";
   int input = 0;
   cin >> input;

   stringstream converterStream;
   converterStream << input;
   string inputAsStr;
   converterStream >> inputAsStr;

   cout << "Integer Input = " << input << endl;
   cout << "String gained from integer = " << inputAsStr << endl;

   stringstream anotherStream;
   anotherStream << inputAsStr;
   int Copy = 0;
   anotherStream >> Copy;

   cout << "Integer gained from string, Copy = " << Copy << endl;

   return 0;
}

<<插入

>>提取

先将一个int型转换为string型,再将string型转换为int型保存


你可能感兴趣的:(C++)