我有下一个:
QString s = "{1.4,3,1.3,33.05}"
我想将它拆分成浮点数并将所有浮点数写入:
vector <Float> v = {1.4,3,1.3,33.05}
你有任何想法,谢谢。
vector<float> MainWindow::digits_from_string(QString data)
{
vector<float> vec;
string s = data.toLocal8Bit().constData();
char *str = new char[s.length()+1];
strcpy(str,s.c_str());
char *pch;
pch = strtok(str," ,{}");
while(pch != NULL)
{
string s;
vec.push_back(stof(s(pch)));
pch = strtok(NULL," ,{}");
}
return vec;
}
我试过这个,但编译器写了stof()函数不存在
答案 0 :(得分:1)
可以试试这个
QString s = "{1.4, 3, 1.3, 33.05}";
QStringList stringList = s.mid(1, s.length() - 2).split(",");
std::vector<float> result;
bool ok;
for(QString item : stringList)
{
float itemNumber = item.toFloat(&ok);
if (ok)
{
result.push_back(itemNumber);
}
}
答案 1 :(得分:-1)
这是你的解决方案:
0 - download
删除QString data = "{1.4,3,1.3,33.05}";
&amp; {
来自字符串:
}
删除空格:
data.remove('{');
data.remove('}');
由data.remove(' ');
分隔:
,
将它们推入向量:
QStringList strList = data.split(QRegExp("[,]"),QString::SkipEmptyParts);
QVector<float> vector;