Delete a QString From QStringList
-
Hello everybody,
imagine i have a QStringList which countains names of persons and i want to delete some names from this QStringList, how can i do it ? i tried
CodeCountry = "FR";QStringList ListName; for (int index =0;index < ListName.length() ; index ++) { if (ListName[index] == "Jon") || (ListName[index] == "Adam"){ ListName.removeAll(ListName[index]); }
but it didn't work, can i get help ? thanks
-
Hello everybody,
imagine i have a QStringList which countains names of persons and i want to delete some names from this QStringList, how can i do it ? i tried
CodeCountry = "FR";QStringList ListName; for (int index =0;index < ListName.length() ; index ++) { if (ListName[index] == "Jon") || (ListName[index] == "Adam"){ ListName.removeAll(ListName[index]); }
but it didn't work, can i get help ? thanks
-
Hello everybody,
imagine i have a QStringList which countains names of persons and i want to delete some names from this QStringList, how can i do it ? i tried
CodeCountry = "FR";QStringList ListName; for (int index =0;index < ListName.length() ; index ++) { if (ListName[index] == "Jon") || (ListName[index] == "Adam"){ ListName.removeAll(ListName[index]); }
but it didn't work, can i get help ? thanks
-
@Zunneh
You must not remove elements from the list while still iterating through it with the code you show.Do as @KroMignon says anyway.
@KroMignon's solution is best but if you have to do something similar with a container that doesn't have a convenient function to do it automatically, you could iterator through it in reverse, starting with the last element.
-
@KroMignon's solution is best but if you have to do something similar with a container that doesn't have a convenient function to do it automatically, you could iterator through it in reverse, starting with the last element.
@mchinand said in Delete a QString From QStringList:
you could iterator through it in reverse, starting with the last element.
Not in this case, given the code as written (which is why I phrased it that way). Even if going in reverse:
QStringList ListName; for (int index = ListName.length() - 1; index >= 0 ; index --) { if (ListName[index] == "Jon") || (ListName[index] == "Adam"){ ListName.removeAll(ListName[index]); }
If this matches more than one line for any given name (I assume it might, else why use
removeAll()
), the list will be shortened by more than one element. When you then do theindex--
for the next iteration in thefor
, you could then have anindex
which is now beyond the newListName.length()
, andListName[index]
will then "crash" (or at minimum revisit already visited elements).Your proposal would work for
QList::removeAt(index)
, but not (safely) forQList::removeAll(list[index])
. -
@mchinand said in Delete a QString From QStringList:
you could iterator through it in reverse, starting with the last element.
Not in this case, given the code as written (which is why I phrased it that way). Even if going in reverse:
QStringList ListName; for (int index = ListName.length() - 1; index >= 0 ; index --) { if (ListName[index] == "Jon") || (ListName[index] == "Adam"){ ListName.removeAll(ListName[index]); }
If this matches more than one line for any given name (I assume it might, else why use
removeAll()
), the list will be shortened by more than one element. When you then do theindex--
for the next iteration in thefor
, you could then have anindex
which is now beyond the newListName.length()
, andListName[index]
will then "crash" (or at minimum revisit already visited elements).Your proposal would work for
QList::removeAt(index)
, but not (safely) forQList::removeAll(list[index])
. -
@Zunneh said in Delete a QString From QStringList:
but it didn't work, can i get help ? thanks
Why you don't simply do this?
ListName.removeAll("Jon"); ListName.removeAll("Adam");
@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 ? -
@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 ...