#include <iostream> #include <sstream> using namespace std; if (1) { int i; stringstream ss_stream; //注意:如果做多次数据转换;必须调用clear()来设置转换模式 ss_stream << "456"; ss_stream >> i; // 首先将字符串转换为int cout << i << endl; ss_stream.clear(); ss_stream << true; ss_stream >> i; // 然后将bool型转换为int;假如之前没有做clear,那么i会出错 cout << i << endl; } if (1) { int a = 1, b = 5; stringstream ss; string name; ss << "face" << a << " " << b; //ss >> name; //这样输出会被空格打断 getline(ss, name); cout << name << endl; for (int i = 0; i < 3; i++) { ss.clear(); ss << "face" << i << " " << i + 5; //ss >> name; getline(ss, name); cout << name << endl; } }
标签:转换,cout,处理,C++,ss,int,字符串,name From: https://www.cnblogs.com/mingfuqishi/p/17494181.html