J
J
Junior0072016-09-20 20:19:44
C++ / C#
Junior007, 2016-09-20 20:19:44

When is this List generally used?

I'm trying to understand the advantages of the std::list structure, it seems to be written that it is able to quickly insert into the middle, but I tested it on Ideone: ideone.com/oFQeXE
List - A clear outsider. It eats a lot of memory, it fills up many times slower than a vector, so it also takes a long time to insert itself.
Vector - 0.2 sec (full execution in: 0.42)
List - 0.11 sec (2.44)
When does it apply then?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaly, 2016-09-20
@vt4a2h

Your experiment is far from ideal. Do about 1000 identical experiments and take the average + if we measure the insertion time, then it is not necessary to take into account the filling time of the container. You need to call resize on the vector and sheet, fill them (not push_back), and then measure the insertion time, and so on about 1000 times, each time with a new vector and sheet. And measure the time after you got the iterator to the right place in the sheet, and not before. Those. only insert time.
Let's not talk about optimization for now.
The sheet to be applied depends on the data and the algorithms used. Where the advantages of the sheet are relevant (read the documentation, which ones), it is applied there. + you still need to be able to use containers (otherwise it will start about the fact that, for example, the vector is slow and eats a lot of memory, or that there is a long access to the index in the sheet).
PS Time can be measured this way: en.cppreference.com/w/cpp/chrono

R
Rou1997, 2016-09-20
@Rou1997

Neither one nor the other applies to me, I use System.Collections.Generic.List, TListor QList, yet NewStringUTF, and the old "rarity" is not needed when I still have something more modern and convenient involved in the project, and the only advantage of STL is "rarities" for me cross-platform and library independence, then out of these 2, std::vector is better for me. supports the operator [], but in general this is extremely rarely required, and I can explain all this with my practical tasks at work, and your benefits seem to be just "sucked out of your finger", so the question does not make sense, explain why you need such speed?

A
Alexey Grichenko, 2016-09-20
@Kalobok

You have a test for linear container filling. One insert in the middle will not affect the results and you will not see the difference between list and vector. Try inserting elements at the beginning of the container - the picture will be completely different.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question