C++ 从QString中提取整数
我需要帮助从Qt QString中获取一些整数。 我有一个文件,我存储了不同的行,与其中的类似:C++ 从QString中提取整数,c++,qt,C++,Qt,我需要帮助从Qt QString中获取一些整数。 我有一个文件,我存储了不同的行,与其中的类似: Fitness: 4 123456789123456789123456789 Fitness: 3 135791357913579135791357913 ……等等。首先,我试图找到适合度最高的一个(在上面的“适合度:4…”行中,4是适合度级别),以及适合度第二高的一个。然后,我将检查具有最高和第二高适应度的数字,并将适应度级别后的27个数字复制到2D数组“readIn”中。这些是我被粘住的部分。
Fitness: 4 123456789123456789123456789
Fitness: 3 135791357913579135791357913
……等等。首先,我试图找到适合度最高的一个(在上面的“适合度:4…”行中,4是适合度级别),以及适合度第二高的一个。然后,我将检查具有最高和第二高适应度的数字,并将适应度级别后的27个数字复制到2D数组“readIn”中。这些是我被粘住的部分。这是我的密码:
void MainWindow::SpliceGenes(){
int secHighest = 0;
int Highest = 0;
int howFar = 0;
QFile file("C:/Users/Quentin/Documents/NeuralNetInfo.txt");
if(file.open(QIODevice::ReadWrite)){
QTextStream stream(&file);
QString text = stream.readAll();
while(text.indexOf(": ", howFar) != -1){//this should make sure it goes through the entire file until it's gotten all of the fitness values.
if(QString.toInt(text[text.indexOf(": ", howFar) + 2]) > Highest){//this should be: if the number after the ': ' is higher than the current
//highest fitness value...
secHighest = Highest;
Highest = QString.toInt(text[text.indexOf(": ", howFar) + 1]);//should be: the new highest value equals the number after the ': '
howFar = text.indexOf(": ", howFar) + 5;//I use howFar to skip past ': ' I've already looked at.
//5是一个随机数,确保它超过了刚刚打开的“:”
}
}
//其余的其实并不重要(我不认为)
ReadNeuralNet(最高、秒最高)
感谢您的帮助。提前感谢toInt()的定义是
int QString::toInt(bool*ok=Q_NULLPTR,int base=10)const
,它不是静态的,这意味着您需要使用一个对象text
是一个QString
,因此您可以使用它的.toInt()
方法
你的代码有很多错误indexOf
返回一个int,它是找到的文本的位置,如果找不到,则返回-1
您可以将mid
与索引(如果找到)结合使用,以剪切出要转换的零件
另外,最好使用readLine
而不是readAll
,并通过QTextStream
循环处理每一行
可能的实施:
QFile file { QStringLiteral("NeuralNetInfo.txt") };
if(file.open(QIODevice::ReadOnly))
{
auto pos { 0 };
QString line { QStringLiteral("") };
QTextStream stream { &file };
while(!stream.atEnd())
{
line = stream.readLine();
pos = line.indexOf(QStringLiteral(": "));
if(pos)
{
pos += 2;
if(pos < line.length())
{
qDebug() << line.mid(pos , 1);
}
}
}
}
toInt()是一个实例方法,不是类方法谢谢!我现在明白了。这是我的新代码,如果有人想看的话:'void main window::strippegenes(){int secHighest=0;int Highest=0;QFile文件(“C:/Users/Quentin/Documents/NeuralNetInfo.txt”);if(file.open(QIODevice::ReadWrite)){QTextStream流(&file);'code>while(!file.atEnd()){QString text=stream.readLine();if((text.mid(10,1)).toInt()>Highest){secHighest=Highest;Highest=(text.mid(10,11)).toInt();}}}ReadNeuralNet(Highest,secHighest);for(int i=0;i
//on 'if(QString.toInt(text[text.indexOf(": ", howFar) + 2]) > Highest){'
error: C2059: syntax error: '.'
error: C2143: syntax error: missing ';' before '{'
//on 'Highest = QString.toInt(text[text.indexOf(": ", howFar) + 1]);'
error: C2275: 'QString': illegal use of this type as an expression
error: C2228: left of '.toInt' must have class/struct/union
//and on the last curly bracket
error: C1903: unable to recover from previous error(s); stopping compilation
QFile file { QStringLiteral("NeuralNetInfo.txt") };
if(file.open(QIODevice::ReadOnly))
{
auto pos { 0 };
QString line { QStringLiteral("") };
QTextStream stream { &file };
while(!stream.atEnd())
{
line = stream.readLine();
pos = line.indexOf(QStringLiteral(": "));
if(pos)
{
pos += 2;
if(pos < line.length())
{
qDebug() << line.mid(pos , 1);
}
}
}
}
"4"
"3"