PDA

View Full Version : problem clear QStringList



jaca
29th May 2008, 20:27
Hi!
I need to clear the velf to show only inserted into the first "for".
when compile and launching program occurs: Segmentation fault.


QStringList velf;
int i = 0;
while (i < list.size()){
if (i <= -1)
break;
if (list.at(i).contains("SPNT",Qt::CaseSensitive)){
qDebug() << "SPNT: " << list.at(i);
i+=1;
}else{
for (int j = i+1, k = 0; j < list.indexOf("SPNT",i); ++j, ++k){
velf.insert(k, list.at(j)); //velf[k] = list[j]; thus also occurs error
}
for (int i = 0; i < velf.size(); ++i)
qDebug() << "VELF: " << velf.at(i);
velf.clear();
i = list.indexOf("SPNT",i);
}
}

Somebody has any idea?
Thanks.

wysota
29th May 2008, 22:38
What is "i" in the first section of code?

jaca
29th May 2008, 22:53
What is "i" in the first section of code?

I edited the post above.
I tried to append, but the QStringList velf still add items.

wysota
30th May 2008, 00:27
If you use insert(), it doesn't overwrite existing items but creates new ones and moves the existing ones. If you want to overwrite items, use the operator[], just make sure you pass valid indices. In your case j points past the end of the list, hence the segfault.

Oh, and get rid of indexOf() from the condition of the for loop. Instead save the value in a separate variable before the loop and use its value instead (in the second for parameter).

jaca
30th May 2008, 17:10
I am sending two archives. Just compile main.cpp. The RON09_posprt.txt archive contains a format that I want to modify. I must is thinking a wrong algorithm.
to execute it makes:
./focus2anp LINE RON09_posprt.txt result.txt

Can anyone help me improve this algorithm?
Thanks.