QStringLIst get duplicates ?
-
-
@aha_1980 Miraculous!
QMap<QString,int> countOfStrings; QStringList listOfStrings; listOfStrings << "a" << "b" << "c" << "a"; for(int i=0; i<listOfStrings.count(); i++) { countOfStrings[listOfStrings[i]]++; } qDebug() << countOfStrings;
But how to read 2 times item out ?
-
-
Hi @sonichy I'm at a loss what you want to do in the first place, what does:
I need to chop(1) every QString in QStringList.
mean?
Do you have a QStringList and you want to reduce all QStrings in that list down to 1 character only?
QStringList myStringList({"abc", "bca", "aaced", "fgh"}); for( QString &str: myStringList) if(!str.isEmpty()) str = str.at(0); //will result in (a, b,a,f);
-
@J.Hilk , @sonichy
At the risk of guessing the OP's intentions, I have a feeling he actually means "how does he find (and remove, or not count?) any duplicates in theQStringList
"? Hmm, maybe not quite, since he's already counting that in hisQMap
. Maybe he wants to output each string the number of times in the count for that string. Maybe I'm talking nonsense and it's to do with the "chop(1)". I love this guessing game.... :) -
@JonB
well QStringList is a complex tool and has for nearly everything a build in function:There's
removeDuplicates
, to remove Duplicates ;-)
There'scount(const T &)
, that returns how often the QString is in the list
and there'sindexOf(const T &, int )
that returns the char index of the first appearence of the QString from the specified int-index on forward -
@sonichy said in QStringLIst get duplicates ?:
@J.Hilk Count(T) is easier and basic than QMap !
Yes, it is easier.
It is also less efficient if you count many different strings.