Solved QString split to QMap
-
@mostefa I did exactly as you said, I asked here to see if people would answer in a different way.
Thank you.
-
@Defohin
But if you use int as key, what would be the benefit for a map over
QStringList ? It will be slower for the look up most likely. :) -
@mrjj How would I get the key from a
QStringList
value?I wanted the
QMap
for that: -
@Defohin
Well it has indexof to go from String to int but
the key is the index..list1[INDEX]
so for this string, index is just the KEYOf cause , if you have 1,2,3,100,200,400
then map is ofc better. -
@mrjj It's not going to have a lot of values, in this case
QStringList::indexOf
would be better?I noticed that
indexOf
is usingQRegularExpression
isn't that going to be slower? -
@Defohin
Ahh sorry. I missed the intersection thing.
No, then QMap is better as indexof is slower due to string searching as if the requirement is to
check if value is there and overwrite or just append if not found.But if the number of items is low, then just focus on good names and
nice structure as performance wise it really wont matter to the app. :) -
@mrjj Based on the other question and this one, I made this:
QStringList a = { "foo", "bar", "fez", "fiz", "foz", "biz" }; QStringList b = { "foo", "bar", "biz" }; QMap<int, QString> c; foreach (QString value, b) { if (! a.contains(value)) return; c.insert(a.indexOf(value), value); } qDebug() << c.keys() << c.values();
It worked, but I don't know if it's going to be faster than:
QMap<int, QString> a = { { 0, "foo" }, { 1, "bar" }, { 2, "fez" }, { 3, "fiz" }, { 4, "foz" }, { 5, "biz" } }; QStringList b = { "foo", "bar", "biz" }; QMap<int, QString> c; foreach (QString value, b) { if (! a.values().contains(value)) continue; c.insert(a.key(value), value); } qDebug() << inter.keys() << inter.values();
-
@mrjj said in QString split to QMap:
@Defohin
Ahh sorry. I missed the intersection thing.
No, then QMap is better as indexof is slower due to string searching as if the requirement is to
check if value is there and overwrite or just append if not found.But if the number of items is low, then just focus on good names and
nice structure as performance wise it really wont matter to the app. :)It would have maximum 200 items.
-
@Defohin
Well its fine code you have above.
Seems totally ok for the task. -
@mrjj You mean
QStringList
orQMap
? -
@Defohin
QMap for the look up. -
Why do you need to use a map for this to begin with (you'd always get worst case complexity for this particular case)? Just use a vector (
QVector
) or a list (QStringList
). -
@mrjj Thank you, solved the problem and it's working like a boss.