Delete a QString From QStringList
-
@KroMignon the example i give was bad
this is the real example, removing names from a QStringList who finish with the letter ' e ' ( if NameList[index].endswith('e') , so there are many names that finish with' e ', removeAll is applied when you know the names, in my case i don't know the Names, the QStringList will append different Names
did you understand ? -
@KroMignon the example i give was bad
this is the real example, removing names from a QStringList who finish with the letter ' e ' ( if NameList[index].endswith('e') , so there are many names that finish with' e ', removeAll is applied when you know the names, in my case i don't know the Names, the QStringList will append different Names
did you understand ?@Zunneh said in Delete a QString From QStringList:
this is the real example, removing names from a QStringList who finish with the letter ' e ' ( if NameList[index].endswith('e') , so there are many names that finish with' e ', removeAll is applied when you know the names, in my case i don't know the Names, the QStringList will append different Names
did you understand ?That is not what you give as implementation!
One possible solution could be usingQStringList::filter()
:const auto toRemove = ListName.filter(QRegularExpression(".*e")); for(const auto &item : toRemove) ListName.removeAll(item);
or
const QRegularExpression filter(".*e"); for(int idx = 0; idx < ListName.size(); ++idx) { if(ListName.at(idx).contains(filter)) { ListName.remove(idx); --idx; } }
-
@Zunneh said in Delete a QString From QStringList:
this is the real example, removing names from a QStringList who finish with the letter ' e ' ( if NameList[index].endswith('e') , so there are many names that finish with' e ', removeAll is applied when you know the names, in my case i don't know the Names, the QStringList will append different Names
did you understand ?That is not what you give as implementation!
One possible solution could be usingQStringList::filter()
:const auto toRemove = ListName.filter(QRegularExpression(".*e")); for(const auto &item : toRemove) ListName.removeAll(item);
or
const QRegularExpression filter(".*e"); for(int idx = 0; idx < ListName.size(); ++idx) { if(ListName.at(idx).contains(filter)) { ListName.remove(idx); --idx; } }
@KroMignon yeah after asking the question, i was in the kitchen and it comes on my mind to do indexx -- if he delete one item ( like the second solution )
on more question, can we put more than one filter ? for example delete name s who finish with 'e', 'y' and 'm' ??@JonB i wil try your solution too
finosh wo
Thanks guys :)
-
@KroMignon yeah after asking the question, i was in the kitchen and it comes on my mind to do indexx -- if he delete one item ( like the second solution )
on more question, can we put more than one filter ? for example delete name s who finish with 'e', 'y' and 'm' ??@JonB i wil try your solution too
finosh wo
Thanks guys :)
@Zunneh said in Delete a QString From QStringList:
on more question, can we put more than one filter ? for example delete name s who finish with 'e', 'y' and 'm' ??
I have not looked to see if accepted, but I would guess either of
const QRegularExpression filter(".*[eym]"); const QRegularExpression filter(".*(e|y|m)");
(I assume you mean "finish with 'e', 'y' or 'm'"!). Regular expression
[abc]
means any one letter of,(ab|c|def)
has to be used if multiple letter sequences required. -
@KroMignon yeah after asking the question, i was in the kitchen and it comes on my mind to do indexx -- if he delete one item ( like the second solution )
on more question, can we put more than one filter ? for example delete name s who finish with 'e', 'y' and 'm' ??@JonB i wil try your solution too
finosh wo
Thanks guys :)
@Zunneh said in Delete a QString From QStringList:
on more question, can we put more than one filter ? for example delete name s who finish with 'e', 'y' and 'm' ??
This is a regular expression, so it is up to you to set it up to match your needs.
For 'e', 'y' and 'm' it would be: const QRegularExpression filter(".*(e|y|m)");
Take a look at https://www.jrebel.com/sites/rebel/files/pdfs/regular-expressions-cheat-sheet.pdf -
stl-iterators are safe for calling erase on. The Qt functionality is identical to the one of
std::vector
.
Given a generic functionbool shouldIDeleteThisString(const QString&)
that returns true if the string should be removed from the list you can use:for(auto i = list.begin(); i!=list.end();){ if(shouldIDeleteThisString(*i)) i=list.erase(i); else ++i; }
-
stl-iterators are safe for calling erase on. The Qt functionality is identical to the one of
std::vector
.
Given a generic functionbool shouldIDeleteThisString(const QString&)
that returns true if the string should be removed from the list you can use:for(auto i = list.begin(); i!=list.end();){ if(shouldIDeleteThisString(*i)) i=list.erase(i); else ++i; }
@VRonin since you're doing iterator fun:
list.erase(std::remove_if(list.begin(), list.end(), shouldIDeleteThisString), list.end());
-
@VRonin since you're doing iterator fun:
list.erase(std::remove_if(list.begin(), list.end(), shouldIDeleteThisString), list.end());
@Christian-Ehrlicher
Indeed, because I looked it up, but it doesn't make it readable, or the way it works very intelligible, IMHO! :) -
@Christian-Ehrlicher
Indeed, because I looked it up, but it doesn't make it readable, or the way it works very intelligible, IMHO! :) -
but it doesn't make it readable
Get used to it, that's the preferred C++ way nowadays ("functional programming").
Regards
@aha_1980 said in Delete a QString From QStringList:
Get used to it, that's the preferred C++ way nowadays ("functional programming").
The major reason for the mortality rise in the developers demographics ... jumping off a tall building ain't no fun ...