Should i use vector or list




















One needs to have access to a pointer first to get their hands-on data. If frequent insertion and deletion occur, and at the same time, memory is not a constraint, then Vector is an ideal choice, whereas in scenarios like frequent access of elements of required with a memory constraint, then Array is a better option. It all depends upon the use case and requirement. An array is always a list in nature, but a vector is a template class and same as a dynamic array.

The array allows both kinds of access, direct and sequential, while Vector only allows sequential access. And this is because of the way these data structures are stored in memory. Since Vector elements are placed in a contiguous memory block, they can be easily traversed using an iterator. An Array is very much tied to the hardware notion of continuous, contiguous memory, with each element identical in size. Other soft rules have to do with the possible performance issues of inserting elements into the middle of a container, whereupon list would be preferable.

Lists are just a wrapper for a doubly-LinkedList in stl, thus offering feature you might expect from d-linklist namely O 1 insertion and deletion. While vectors are contagious data sequence which works like a dynamic array. S- easier to traverse. List is Doubly Linked List so it is easy to insert and delete an element. We have to just change the few pointers, whereas in vector if we want to insert an element in the middle then each element after it has to shift by one index.

Also if the size of the vector is full then it has to first increase its size. So it is an expensive operation. So wherever insertion and deletion operations are required to be performed more often in such a case list should be used. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 11 years, 9 months ago. Active 6 months ago.

Viewed k times. I noticed in Effective STL that vector is the type of sequence that should be used by default. What's does it mean? It seems that ignore the efficiency vector can do anything.

Improve this question. Though it's not what you asked, it's worth pointing out that defaulting to vector also means you can easily interact with older code, C libraries, or non-template libraries, since vector is a thin wrapper around the "traditional" dynamic array of a pointer and size.

Bjarne Strostrup actually made a test where he generated random numbers and then added them to a list and a vector respectively. Reason being that memory acces is slow and caching works better for sequential data. It's all available in his keynote from "GoingNative " — evading. If you want to see the keynote by Bjarne Stroustrup that evading mentioned, I found it here: youtu.

Add a comment. Active Oldest Votes. Pre-allocates space for future elements, so extra space required beyond what's necessary for the elements themselves. Each element only requires the space for the element type itself no extra pointers.

Can re-allocate memory for the entire vector any time that you add an element. Insertions at the end are constant, amortized time, but insertions elsewhere are a costly O n.

Erasures at the end of the vector are constant time, but for the rest it's O n. You can randomly access its elements. Iterators are invalidated if you add or remove elements to or from the vector. You can easily get at the underlying array if you need an array of the elements.

No pre-allocated memory. The memory overhead for the list itself is constant. Each element requires extra space for the node which holds the element, including pointers to the next and previous elements in the list.

Never has to re-allocate memory for the whole list just because you add an element. Insertions and erasures are cheap no matter where in the list they occur. It's cheap to combine lists with splicing. You cannot randomly access elements, so getting at a particular element in the list can be expensive. Iterators remain valid even when you add or remove elements from the list.

If you need an array of the elements, you'll have to create a new one and add them all to it, since there is no underlying array. In the list case, the processor spends its whole time waiting for data being fetched from memory to the cache. In the case of random insert, in theory, the list should be much faster, its insert operation being in O 1 versus O n for a vector. Then, random values are inserted at a random position in the container.

The random position is found by linear search. In both cases, the complexity of the search is O n , the only difference comes from the insert that follow the search. When, the vector should be slower than the list, it is almost an order of magnitude faster.

Again, this is because finding the position in a list is much slower than copying a lot of small elements. This time, list outperforms vector by an order of magnitude! The performance of random insert in a list are not impacted much by the size of the data type, where vector suffers a lot when big sizes are used. We can also see that list doesn't seem to care about the size of the collection.

It is because the size of the collection only impact the search and not the insertion and as few search are performed, it does not change the results a lot. If the iterator was already known no need for linear search , it would be faster to insert into a list than into the vector. In theory, random remove is the same case than random insert. Now that we've seen the results with random insert, we could expect the same behavior for random remove.

Then, random values are removed from a random position in the container. Again, vector is several times faster and looks to scale better. Again, this is because it is very cheap to copy small elements. The behavior of random remove is the same as the behavior of random insert, for the same reasons. You can request a component with a positive integer or by name.

Like [[ , this can only be used to access a single component, but it is even more limited: You must specify the component by name. Highly recommended! What succeeds vs. What if you try to retrieve element 2 alone? Does [[ even work on atomic vectors?

Compare and contrast the results from the various combinations of indexing method and input object. Many people are shocked to learn that the garden variety R object is a vector. This often requires an adjustment for people coming from other languages. Your code can be simpler than you think! Consider how to square the integers 1 through n. The brute force method might look like:. Which is great. You get used to it soon enough. This happens for atomic vectors, but not, in general, for lists.

This makes sense because, in general, there is no reason to believe that the same sorts of operations make sense for each component of a list. Unlike atomic vectors, they are heterogeneous. So, how do you apply a function elementwise to a list?! Use purrr::map! The first argument is the list to operate on. The second is the function to apply.



0コメント

  • 1000 / 1000