之前的sscanf和sprintf
sscanf函数原型为int sscanf(const char *str,const char *format,…),将参数str的字符串根据参数format字符串来转换并格式化数据,转换后的结果存于对应的参数内;
#include
#include
using namespace std;
int main(){
char s[] = "123.432,432";
double f1;
int f2;
int n;
sscanf_s(s, "%lf,%d%n", &f1, &f2, &n);
cout << f1 << " " << f2 << " " << n << endl;
system("pause");
return 0;
}
sprintf函数原型为 int sprintf(char *str, const char *format, …),作用是格式化字符串;
void sprintf() {
char str[256] = { 0 };
int data = 1024;
//将data转换为字符串
sprintf_s(str, "%d", data);
cout << str << endl;
//获取data的十六进制
sprintf_s(str, "0x%X", data);
cout << str << endl;
//获取data的八进制
sprintf_s(str, "0%o", data);
cout << str << endl;
const char *s1 = "Hello";
const char *s2 = "World";
//连接字符串s1和s2
sprintf_s(str, "%s %s", s1, s2);
cout << str << endl;
}
sstream
sstream定义了三个类:istringstream、ostringstream 和 stringstream,分别用来进行流的输入、输出和输入输出操作
stringstream ss;
double price = 380.0;
char *ps = " for a copy of the ISO/EIC C++ standard!";
ss.precision(2);//精度
ss << fixed;//固定位数
ss << "Pay only CHF " << price << ps << endl;
cout << ss.str() << endl;//将缓冲区的内容转化为字符串
string word;
while(ss>>word){
cout << word << endl;
}
string s = "12345";
stringstream ss;
int x;
ss << s;
ss >> x;
cout << x << endl;