Should I use deque instead of vector if i’d like to push elements also in the beginning of the container? When should I use list and what’s the point of it?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use
dequeif you need efficient insertion/removal at the beginning and end of the sequence and random access; uselistif you need efficient insertion anywhere, at the sacrifice of random access. Iterators and references tolistelements are very stable under almost any mutation of the container, whiledequehas very peculiar iterator and reference invalidation rules (so check them out carefully).Also,
listis a node-based container, while adequeuses chunks of contiguous memory, so memory locality may have performance effects that cannot be captured by asymptotic complexity estimates.dequecan serve as a replacement forvectoralmost everywhere and should probably have been considered the “default” container in C++ (on account of its more flexible memory requirements); the only reason to prefervectoris when you must have a guaranteed contiguous memory layout of your sequence.