我想采用这种格式的用户输入—— (xx, xx, x) 其中 x 是一些任意的双精度值,并且在每个值之后总是有一个逗号和空格。
我已经将此输入作为字符串值来解析它并检查用户是否没有放任何时髦的东西。我无法将字符串转换为双精度,然后将这些值添加到双精度类型的向量中。我尝试使用 atof 函数,但我对如何在第一个元素之后添加到向量感到困惑。
这就是我到目前为止在 C++ 中所拥有的。
string vector1, vector2;
//asks for first vector
cout << "Enter first vector"<<endl;
getline(cin,vector1);
vector <double> Vector1;
//vector1 without the parantheses
vector1= vector1.substr(1,vector1.length()-2);
cout<< vector1<<endl;
Vector1.push_back(atof(vector1.c_str()));
cout<< Vector1[0];
有任何想法吗?我正在考虑制作一个循环,但我不知道如何告诉 atof 函数在逗号和空格之后继续。