Friday, May 11, 2012

QVector Advantages


In our workplace  we use stl as much as possible. Even when coding in qt we are advised to import and use stl containers. I googled this issue to know justifications behind this favourism. What I found is STL is a clear winner if speed is single most important concern. On the other hand qtl has many advantages of it’s own. Let me elaborate these points with an example of vector. QVector is more easier to use than stl::vector. Qvectors are more light weight. Whenever we copy or pass qvector , only a pointer to vector is passed. A deepcopy is used only when editing data. QVector occupies more memory to prevent use of memory allocation operations each and every time expansion is required. It uses realloc() to grow by increments of 4096 bytes. Qvector has lots of interesting functions to make the life easier for developers. It has a squeeze option to remove extra spaces allocated. So I believe it’s advisable to use qvector as much as possible. And in case you want to use stl:: vector , you can always convert to stl::vector like this
 QVector vector;
 vector << 1.2 << 0.5 << 3.14;
 std::vector stdvector = vector.toStdVector();  

No comments:

Post a Comment