C++字符串处理

 

 

 

 

 

#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;
}
}

 

posted @ 2023-06-20 17:12  冥府骑士格斯  阅读(3)  评论(0编辑  收藏  举报