linked lists vs arrays [duplicate] - c++

This question already has answers here:
When to use a linked list over an array/array list?
(15 answers)
vector vs. list in STL
(17 answers)
Closed 8 years ago.
sorry if this been asked already...
Why and when Should I use linked lists over vectors? I just don't like all those pointer overheads...
From my knowledge: vector is faster, more compact because there's no extra pointers, and is easier to implement; also I think that linked lists do not exploit the principle of spatial locality because nodes are in totally random memory locations so your code becomes slower... so when you are using linked lists you are increasing your cache misses which you don't want to do...
Of course the advantage of lists is that you can avoid overflows with dynamic memory allocation...
In summation, my question is: where should you use, if ever, linked lists over vectors? which data struct do you prefer more?

Linked Lists are for situations where you want to insert or remove an item without shifting or insert/push or pop item in constant time and when you don't know the number of elements and maybe don't need a random access. for more information see this .

Related

Should I use linked lists or arrays when sorting 100 million elements? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want to implement algorithms like quicksort, mergesort, etc., while working with a big data files, like 100 million elements. I can't use std::vector, std::sort, or anything like that, since this is a school assignment to get to know these specific algorithms. I can only use things that I will only write on my own.
Should I implement sorting algorithms using linked lists or arrays? Which from these two is more efficient in term of working with big data? What are the advantages of using one of them?
If the number of elements is large, the better option would be an array (or any type that has contiguous memory storage, i.e. a std::vector, allocating with new [], etc.). A linked list usually does not store its nodes in contiguous memory. The contiguous memory aspect leads to better cache friendliness.
In addition to this, a linked list (assuming a doubly-linked list), would need to store a next and previous pointers to the next and previous elements for each data item, thus requiring more memory per data item. Even for a singly-linked list, a next pointer has to exist, so even though less overhead than a doubly-linked list, it is still more overhead than an array.
Another reason that isn't related to efficiency why you want to use an array is ease of implementation of the sorting algorithm. It is more difficult to implement a sorting algorithm for a linked list than it is for an array, and especially an algorithm that works with non-adjacent elements.
Also, please note that std::sort is an algorithm, it is not a container. Thus it can work with regular arrays, std::array, std::vector, std::deque, etc. So comparing std::sort to an array is not a correct comparison.

Vectors vs Array in C++ [duplicate]

This question already has answers here:
Using arrays or std::vectors in C++, what's the performance gap?
(21 answers)
Closed 3 years ago.
I am working on a Project in which the I am using vectors since the size of array is unknown initially. Does using arrays instead of vectors reduce the run time of code?If Yes, Then How can i initialize/declare the array of unknown size i.e. Size of array is variable(based on input)? "OR" Is it better to use vectors only?
Note :- I want to know Which better reduces the Execution time of Program.
In programming as in life there is no free meal... Keep that in mind all the time. If you want nice and convenient features you have to pay a price.
std::vector will add some complexity to your code you don't see. Adding items to your std::vector does more, then just writing a value. It may allocate new memory and copy the old values to it. And several more things you won't really see.
Switching to std::array won't give you the boost you might looking for. It is a bit simpler then std::vector. It is the way to got, when you are looking for an supplement of an plain c array. But still, it will add complexity too.
So my advice is and you will find similar once in good books. Try to optimize your code on algorithm base and not on the implementation. There is much more potential in possible flawed algorithms or there may be much better once. The implementation won't give you the ground braking boost.

Why the output shows file 2 first? [duplicate]

This question already has answers here:
Is the order of items in a hash_map/unordered_map stable?
(2 answers)
Closed 7 years ago.
Context: I need this data structure to find the keywords of a particular file at a node. So the map is having file name and vector to store the keywords of that file.This is basically a small code i am using in mpi to find relationship between files at different nodes in a parallel fashion.
The order of the elements of an std::unordered_map is not stable, which explains your output.
Read this answer for more and how to read the elements. Also next time search before asking a question and of course (almost) never post images of code, but use code tags.
From the ref:
Internally, the elements are not sorted in any particular order, but organized into buckets.

C++ use array pointer vs vector? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Using arrays or std::vectors in C++, what's the performance gap?
I just wanna know which of them are the faster and use less resources? I think that vector is more reliable and secure but a pointer to an array is faster. I want to re-size the array (add new elements, so increment it by 1 or remove elements from it). A vector has its functions for that while a pointer needs one created by me.
I don't know which one to choose. What do you advise me? Thanks!
According to Bjarne Stroustrup, you should use vector over Array unless you have a really good reason to use an array.
The c++ standard libaries have been optimized to be as fast as possible all the while providing necessary functions so that you do not have to implement them. Save yourself the time and worry and just use a vector.
If there are any discrepancies on speed they will be negiligble in the big picture :)

Thoughts on how to implement? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 months ago.
Improve this question
I am porting some very old c-code into c++ and I've run across a linked list implemented within an array. The element is a simple structure:
struct element
{
void *m_ptrData;
short m_nextEntry;
short m_prevEntry;
};
As an array, there is quick access to the data, if you know the index. The linked list aspect lets the elements be moved around, and "deleted" from the list. Elements can be moved in the list, based on frequency of use (up for MRU and down for LRU).
I like to find a better way to implement this than using another array. I'd like to use STL, but I'm not certain which container is best to use.
Any one have any thoughts?
Since this is a linked list, you should probably use std::list...
The rule of thumb is that you want to use a linked list when you need to insert elements into random positions in the list, or delete random elements from the list. If you mainly need to add/delete elements to/from the end of the list, then you should use std::vector. If you need to add/delete elements to/from either beginning or the end of the list, then you should use std::deque.
Keep in mind, we are talking about probabilities here. If you need to insert an element into the middle of an std::vector once in a blue moon, that will probably be ok. But if you need to do this all the time, it will have a major impact on performance, because the vector will need to constantly move its elements, and probably reallocate its memory too.
On the other hand, the advantage of using a vector is that its elements are contiguous in memory, which greatly improves performance if you simply need to traverse them in order because of caching.
Since the data in this list is pointers, why bother with a linked list at all? For small PODs, std::vector is usually the best first bet, and due to the better locality of its data playing nicely with processor caches it often out-performs a linked list even where, in theory, a linked list should be better. I'd pick std::vector until some profiling would show that there is a performance problem and std::list performs better.
See here:
http://linuxsoftware.co.nz/cppcontainers.html
There's a flow chart to help you choose the right container at the bottom.