QString split to QMap
-
Hi there, I was wondering, how to transform a
QString::split
into aQMap<int, QString>
where theint
is the position of the value and theQString
is the value in aQMap
?QString identifier = "foo.bar.biz.boz"
It shoud be:
QMap<int, QString> result = { { 0, "foo" }, { 1, "bar" }, { 2, "biz" }, { 3, "boz" } }
-
Why not just use list ?
QString str ="foo.bar.biz.boz" QStringList list1 = str.split('.'); list1[0] is foo list1[1] is bar etc...
-
you can do something like this:
```
QString identifier = "foo.bar.biz.boz";
QStringList stringList = identifier.split("."); qDebug() << stringList; QMap<int,QString> map; for(int i=0;i < stringList.size();i++) { map.insert(i,stringList.at(i)); } qDebug() << map;
-
you can do something like this:
```
QString identifier = "foo.bar.biz.boz";
QStringList stringList = identifier.split("."); qDebug() << stringList; QMap<int,QString> map; for(int i=0;i < stringList.size();i++) { map.insert(i,stringList.at(i)); } qDebug() << map;
-
@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. :) -
@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 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();
-
@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 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.
-
@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.
-
Hi there, I was wondering, how to transform a
QString::split
into aQMap<int, QString>
where theint
is the position of the value and theQString
is the value in aQMap
?QString identifier = "foo.bar.biz.boz"
It shoud be:
QMap<int, QString> result = { { 0, "foo" }, { 1, "bar" }, { 2, "biz" }, { 3, "boz" } }
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
).