Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在C+中读取由空格分隔的字符串中的多个双精度数字+;?_C++_String_Double - Fatal编程技术网

C++ 如何在C+中读取由空格分隔的字符串中的多个双精度数字+;?

C++ 如何在C+中读取由空格分隔的字符串中的多个双精度数字+;?,c++,string,double,C++,String,Double,我需要读取字符串,例如: string str=“3-90.2 85.54 93.0”函数strtok()对于从较大字符串中解析内容非常有用。一旦pch指向每个double,然后使用对atof()的调用将其转换 您可以使用stringstream string str = "3 - 90.2 85.54 93.0"; stringstream ss(str); int num; ss >> num; //read 3 char c; ss >> c; /

我需要读取字符串,例如:

string str=“3-90.2 85.54 93.0”函数strtok()对于从较大字符串中解析内容非常有用。一旦pch指向每个double,然后使用对atof()的调用将其转换


您可以使用stringstream

string str = "3 - 90.2 85.54 93.0";
stringstream ss(str);
int num;   ss >> num; //read 3
char c;    ss >> c;    // read "-"
double d1; ss >> d1;   // read 1
double d2; ss >> d2;   // read 2
double d2; ss >> d2;   // read 3

使用
istream\u迭代器
s

std::vector<double> xyzzy{std::istream_iterator<double>{std::istringstream{str}}, std::istream_iterator{}};

您甚至可以使用while循环拆分字符串,字符串中的浮动没有限制,因为这将随着字符串结束而结束,请执行以下操作:

int i=0, p=0;
float fdigit=0;
char string[]="9.0 5.4 2.6", floats[10];
while (floats[i]!='\0')
{
while (floats[i]!=' ')
{
floats[p]=floats[i];
p++;
i++;
floats[p]='\0';
fdigit=atof(floats);
cout<<fdigit<<"\n";
}
if (floats[i]==' ')
i++;
}
inti=0,p=0;
浮点fdigit=0;
字符字符串[]=“9.0 5.4 2.6”,浮动[10];
while(浮动[i]!='\0')
{
while(浮动[i]!='')
{
浮动[p]=浮动[i];
p++;
i++;
浮点数[p]='\0';
fdigit=atof(浮动);
cout
std::string str=“3-90.2 85.54 93.0”;
std::stringstream ss(str);
int num;ss>>num;
字符c;ss>>c;
std::载体arr;
arr.reserve(num);
对于(int i=0;i>d;
arr.推回(d);
}

您只需重复代码即可:

double x = stod(str, &sz);
str = str.substr(sz);
double y = stod(str, &sz);
str = str.substr(sz);
double z = stod(str, &sz);
str = str.substr(sz);
double x1 = stod(str, &sz);
请参阅文档:

double stod(const string &  str, size_t * idx = 0);
  • str
    :表示浮点的字符串对象 号码
  • idx
    :指向size\u t类型的对象的指针,其值由 函数将下一个字符的位置设置为 数值。此参数也可以是空指针,其中 如果不使用,则为

这是dobg的C方式,不是C++方式。但是正如你所看到的,有很多方法来完成同样的事情。偏好、代码清晰性和熟悉度通常决定了最终的结果。
std::string str = "3 - 90.2 85.54 93.0";
std::stringstream ss(str);
int num;   ss >> num;
char c;    ss >> c;
std::vector arr;
arr.reserve(num);
for (int i = 0; i < num; ++i)
{
    double d; ss >> d;
    arr.push_back(d);
}
double x = stod(str, &sz);
str = str.substr(sz);
double y = stod(str, &sz);
str = str.substr(sz);
double z = stod(str, &sz);
str = str.substr(sz);
double x1 = stod(str, &sz);
double stod(const string &  str, size_t * idx = 0);