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 :)
Related
This question already has answers here:
Find mapped value of map
(6 answers)
Closed 1 year ago.
std::unordered_map::find search for the certain key in the unordered_map, and is there a function to search a certain value?
Definitely I can write some simple loop to do it but maybe something already exist for that that?
No, not really. The map entries (key-value pairs) are not arranged according to their values; nor are the values stored separately from the keys etc. Or rather - the standard doesn't guarantee any of that, and all popular implementations don't offer this.
You're just going to have to use a linear search... or a different/additional data structure which supports the kind of searches you need.
Also remember that std::unordered_map is quite slow in practical, non-asymptotic terms, so if performance is a consideration - definitely look for alternatives.
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.
This question already has answers here:
Why use pointers? [closed]
(17 answers)
Closed 4 years ago.
As someone new to programing (C++) , I came across pointer which gave me a headache doing it as I never seen the point of using it. I tried to look at other answer on stack overflow but it was a bit confusing.It would be great if someone could explain why we use pointers and in a way to make it easier to understand.
One reason is so that we can directly manipulate the content the pointer address is holding.
There are a number of uses for pointers. They represent a way for code to manipulate memory directly primarily, but can also be used for things like arrays. In modern C and C++, you would usually use pointers for accessing hardware to control it through custom structs, for example.
However, for some other complex and for recursive types, pointers are still the easiest and only way to model them. A linked list, for example, could have any number of items, but there's no really good way to refer to the next (or previous) item in C or C++ without using a pointer to it.
Allocating larger data structures will also usually require a pointer to refer to it as well, since these large allocations won't fit on the stack.
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 .
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
When do you prefer using std::list<T> instead of std::vector<T>?
I just watched the recording of the GoingNative'12 talk by Bjarne Stroustrup. And I'm a bit confused.
In this talk he in particular discusses the vector vs list question and suggest that in many cases vector is faster even if you insert and remove intensively to/from the middle, as compilers can optimize a lot of things and like compact structures. And the conclusion(as I understand it) is: first use vector and later think whether you need something else. That sounds reasonable, but taking into account the first observation, what criteria I should take into account? I always thought that if you insert/remove intensively - use list. Similar things are suggested in some topics here. See
Relative performance of std::vector vs. std::list vs. std::slist?
and
vector vs. list in STL
And now according to Stroustrup I was wrong.
Of course I can write a couple of tests and try to figure out what to use in each particular situation, but is there a theoretical way?
The most important motivation for preferring std::list over std::vector is the validity of iterators, not performance. If at the time you're inserting or erasing, you have other iterators into the container, then you probably need std::list, since it insertion doesn't invalidate any iterators, and erasure only invalidates iterators to the element being erased. About the only time std::list will win on performance is when copy and assignment are extremely expensive, and in such cases, it's often a better choice to modify the contained class to reduce the cost of copy and assignment, rather than switching to std::list.