使用istringstream提取

Extraction using istringstream?

本文关键字:提取 istringstream 使用      更新时间:2023-10-16

我有以下内容:

char op; double x, y, z;
istringstream iss("v 1.0 2.0 3.0", istringstream::in);
iss>>op>>x>>y>>z;

但是在输出x,y和z的值时,它们都返回0?

更新:

我猜它正在工作,但我输出它为:

int length=wsprintf(result," V is %d, %d, %d ", x, y, z);
TextOut(hdc,0,0,result,length);

没有显示正确的值

但是,如果值是int类型,例如:

char op; int x, y, z;
istringstream iss("v 1 2 3", istringstream::in);
iss>>op>>x>>y>>z;

%d格式说明符期望是int,但x, yz类型是double。如果类型和格式说明符不匹配,则行为是未定义的。注意,wsprintf的参考页面似乎没有任何double的格式说明符。

建议使用std::wostringstreamstd::wstring代替:

std::wostringstream ws;
ws << L" V is " << x << L"," << y << L"," << z;
const std::wstring result(ws.str());
TextOut(hdc,0,0,result.c_str(), result.length());

谢谢。

对于其他人,这是我为正确输出值所做的操作。

int length=sprintf_s(result," V is %0.1f, %0.1f, %0.1f ", x, y, z);
TextOut(hdc,0,0,result,length);

通用语法"%A"。"B"指小数点&amp前的A位;小数点后B位。

谢谢。