PDA

View Full Version : std vs qt containers



ravas
9th July 2015, 20:33
I will have a function that places actions one by one into a "map" and then returns it.
The receiving function will use different "lists" to loop over that map.

Do you recommend QMap, QHash, std::map or std::unordered_map?

For short lived lists, with no inserts or removals, that will be used once in a ranged for loop,
do you recommend QList or std::vector?
I considered a simple array, but I want to just keep reusing the same variable name.

The lists will be used in combination with the map to created toolbars/menues in loops.

example idea:


vector<string> a_vector;

a_vector = {"a1", "a2", "a3"}

for (string str : a_vector)
{
a_map[str]->setCheckable(true);
menu->addAction(a_map[str]);
toolbar->addAction(a_map[str]);
}

... menu and toolbar change ...

a_vector = {"a4", "a5", "a6", "a7"}

for (string str : a_vector)
{
a_map[str]->setCheckable(true);
menu->addAction(a_map[str]);
toolbar->addAction(a_map[str]);
}

jefftee
9th July 2015, 20:40
I personally choose to use Qt containers when writing stuff that uses Qt, but I think the std containers are slightly faster. For such a small, static list of items, I don't think it will matter.

Edit: I would use QMap and QList for what you have stated you want to do.

stampede
9th July 2015, 20:49
(...) do you recommend QList or std::vector?
If performance is not a concern, use the one you like better (API, personal taste etc.). If you are concerned about performance, then profile and choose the one better suited for the job.

d_stranz
10th July 2015, 04:24
I personally like my computational code to be even more portable than Qt, so I tend to use STL containers for most things. However, I have recently discovered that std::iterator is about 3 - 5 times slower in release mode and several orders of magnitude slower in debug mode than simple index access in Visual Studio 2013 because of extra bounds checking that goes on under the hood. I haven't done any comparable testing on Qt containers and iterators.

anda_skoa
10th July 2015, 08:37
Don't use ranged for with a Qt container, at least not with non-const one.
On a non-const container, ranged for will call non-const begin/end, which will cause a Qt container to deep copy if its reference counter is > 1.

http://www.dvratil.cz/2015/06/qt-containers-and-c11-range-based-loops/

In general, use vector over list, e.g. QVector or std::vector instead of QList, unless you need the list features.

https://marcmutz.wordpress.com/effective-qt/containers/

Cheers,
_

ravas
10th July 2015, 21:01
Wow... there is a lot of information in those blogs. :eek:

Thanks all.