Why is QString::remove not working as expected?
-
wrote on 27 Nov 2022, 08:04 last edited by
Hi, I am trying to remove certain chars form a QString however the remove function is doing something funny:
// remove all instances of power, wheather negative positive for (int i{};i<_numerator.size();++i){ if (_numerator[i] == coeff){ if (_numerator[i+2] == '-'){ _numerator.remove(i+1, 3); } else{ _numerator.remove(i+1, 2); } } }
For given a string of "15x^-2+10x^-3" I want to modify it to "15x+10x". Can someone help me as to why this doesn't work?
-
Hi and welcome to the forums
It is a classic mistake.
when you loop over a container using its size and in that loop, remove elements, fun things are bound to happen.Try to loop it reversed and see if that cures it.
If not, you need to copy on the fly the parts you want and then skip the unwanted ones.
-
Hi, I am trying to remove certain chars form a QString however the remove function is doing something funny:
// remove all instances of power, wheather negative positive for (int i{};i<_numerator.size();++i){ if (_numerator[i] == coeff){ if (_numerator[i+2] == '-'){ _numerator.remove(i+1, 3); } else{ _numerator.remove(i+1, 2); } } }
For given a string of "15x^-2+10x^-3" I want to modify it to "15x+10x". Can someone help me as to why this doesn't work?
wrote on 27 Nov 2022, 08:49 last edited by JonB@HFT_developer
Hello and welcome.Assuming
coeff
is the^
character you know that is at_numerator[i]
. Yet you look for any-
symbol ati+2
when it would come immediately after the^
which would bei+1
. And you only start deleting fromi+1
which is after the^
so that will be retained, which you do not want.Untested, but don't you want:
if (_numerator[i+1] == '-'){ _numerator.remove(i, 3); } else{ _numerator.remove(i, 2); }
?
Note that you are making assumptions here like (a) once you meet a
^
or^-
there will be at least one character after it and (b) only one character after it, e.g.15x^-20+10x^333
would not be replaced correctly at either^...
sequence. -
@HFT_developer
Hello and welcome.Assuming
coeff
is the^
character you know that is at_numerator[i]
. Yet you look for any-
symbol ati+2
when it would come immediately after the^
which would bei+1
. And you only start deleting fromi+1
which is after the^
so that will be retained, which you do not want.Untested, but don't you want:
if (_numerator[i+1] == '-'){ _numerator.remove(i, 3); } else{ _numerator.remove(i, 2); }
?
Note that you are making assumptions here like (a) once you meet a
^
or^-
there will be at least one character after it and (b) only one character after it, e.g.15x^-20+10x^333
would not be replaced correctly at either^...
sequence.wrote on 27 Nov 2022, 08:54 last edited by HFT_developer@JonB yeah your right. Could you recommend a better algorithm?
-
@JonB yeah your right. Could you recommend a better algorithm?
wrote on 27 Nov 2022, 08:59 last edited by@HFT_developer
If I were writing this I would probably use the overload QString &QString::remove(const QRegularExpression &re) which uses a regular expression to do all of the removals in one call:Removes every occurrence of the regular expression re in the string
There is a bit of an issue specifying the
^
character because that can mean "beginning of string` in a regular expression. Again untested, but how does this behave:_numerator.remove(QRegularExpression("\\^-?\\d+"));
? That is intended to remove (a) a literal
^
(b) optionally immediately followed by a single-
and (c) immediately followed by one or more digits. -
@HFT_developer
If I were writing this I would probably use the overload QString &QString::remove(const QRegularExpression &re) which uses a regular expression to do all of the removals in one call:Removes every occurrence of the regular expression re in the string
There is a bit of an issue specifying the
^
character because that can mean "beginning of string` in a regular expression. Again untested, but how does this behave:_numerator.remove(QRegularExpression("\\^-?\\d+"));
? That is intended to remove (a) a literal
^
(b) optionally immediately followed by a single-
and (c) immediately followed by one or more digits.wrote on 27 Nov 2022, 09:48 last edited by@JonB I checked and that works well. I have another question about converting a list to another. In STL you have
std::transform
to do that. What is the equivalent for this in qt?For example, I want to convert a
QList<QString>
toQList<double>
, in this case I can guarantee that is will contain something like{"15.5", "19.9", "25.5"}
there wont be any non numbers. Right now I am using a simple loop:QList<QString> _splitter = data.split(coeff); QList<double> ret; // transform into double from QString for (int i{};i<_splitter.size();++i){ ret.push_back(_splitter[i].toDouble()); }
what are your thoughts on this?
-
Hi,
This is valid. You could also use std::transform.
1/7