Vector vs deque | is deque superior? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Vector vs deque | is deque superior?

Please find below code: Vector resizes in case of capacity is full and hence copy constructor gets called with each resize. This problem is not observed with deque in shared code. Does this mean vector can be ignored and it's always a good choice to opt for deque? Additionally, deque provides push_front as well. https://code.sololearn.com/c16SFl2nx64q/?ref=app https://code.sololearn.com/c16SFl2nx64q/?ref=app

10th May 2021, 7:55 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
3 Answers
+ 7
From https://www.cplusplus.com/reference/vector/vector "Compared to the other dynamic sequence containers (deques, lists and forward_lists), vectors are very efficient accessing its elements (just like arrays) and relatively efficient adding or removing elements from its end. For operations that involve inserting or removing elements at positions other than the end, they perform worse than the others, and have less consistent iterators and references than lists and forward_lists."
10th May 2021, 8:19 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 6
That highly depends on what you're doing. Generally a deque outperforms a vector when working with very big data types, randomly inserting or removing into the container and especially adding data to the front of the container. deque is basically a linked list of vectors so it does not require a full reallocate when pushing something to the front, but iterating through it comes at a higher cost. I would say, check out the data yourself: https://baptiste-wicht.com/posts/2012/12/cpp-benchmark-vector-list-deque.html
10th May 2021, 8:18 AM
Dennis
Dennis - avatar
0
Thank you CarrieForle and Dennis
10th May 2021, 7:00 PM
Ketan Lalcheta
Ketan Lalcheta - avatar