Question

In: Computer Science

When should you choose a deque over a vector and a vector over a deque in...

When should you choose a deque over a vector and a vector over a deque in c++

Solutions

Expert Solution

One should choose vector if insertion or deletions are required mostly in end like implementing a Stack.

  • Vector provides insertion and deletion at middle and end only. Whereas, deque provides operations for insertion at front, middle and end. That is, apart from push_back() and pop_back() APIs jus like vector, deque also has push_front() and pop_front() API to add and delete elements from front.
  • Vector provides good performance while insertion and deletion at end only and bad performance for insertion and deletion at middle.
  • Deque provides same kind of performance as vector for insertion & deletion at end and middle. Apart from that deque provides good performance for insertion and deletion at front also.
  • As Vector stores elements contiguously, where as deque internally contains a list of memory chunks which store elements contiguously. Due this basic architectural difference between vector and deque following things happen,
  • Performance of addition and deletion at end for vector is better than deque.
  • No Iterator invalidation happens in deque for insertion and deletion at front and end because like vectors, deque doesn’t have to shift elements from one memory to another in case current allocated memory is not sufficient to store the newly added element.
  • Iterator invalidation happens in deque just like vector, if insertion or deletion takes place in the middle.
  • Just like vector, deque also supports random access operations i.e. operator [] and at() function. Although performance of random access in deque will be little slower than vector.
Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

Related Solutions

Question 2 Indicate six key differences between a deque and a vector. When should you choose...
Question 2 Indicate six key differences between a deque and a vector. When should you choose one over the other? Explain how a deque works internally by providing a thorough explanation of how the item insertion happens.
Indicate six key differences between a deque and a vector. in C++
Indicate six key differences between a deque and a vector. in C++
Let V be a vector space of dimension 1 over a field k and choose a...
Let V be a vector space of dimension 1 over a field k and choose a fixed nonzero element voe V, which is therefore a basis. Let W be any vector space over k and let woe W be an arbitrary vector. Show that there is a unique linear transformation T: V → W such that T(v)= wo. [Hint: What must T(Avo) be?)
1: When projects are mutually exclusive, you should choose the project with the: Assuming an interest...
1: When projects are mutually exclusive, you should choose the project with the: Assuming an interest rate of 14%, what is the net present value of an investment with the cash flows indicated in the table? CF0 -$193,000   CF1 $50,235   CF2 $62,850   CF3 $62,850   CF4 $48,910   CF5 $87,415 2- Assuming a firm has unlimited access to funds, what is the proper accept or reject decision when using internal rate of return (IRR)? please make sure so I can read your...
When would you choose to use containers in Windows and when would you choose to use...
When would you choose to use containers in Windows and when would you choose to use containers in Linux? How are they different? How are they similar?
When should you use and object-oriented language over a procedural language and vice versa?
When should you use and object-oriented language over a procedural language and vice versa?
When does the public good exceed personal medical privacy? You should choose a specific point of...
When does the public good exceed personal medical privacy? You should choose a specific point of view and defend it. Discuss issues such as medical research, biosurveillance, and the fact that poor health choices made by an individual impact all of society. Use at least 3 external sources to support your arguments. Target length: 400 words.
When should an ANOVA be used? Choose all that apply. You have two numerical data sets...
When should an ANOVA be used? Choose all that apply. You have two numerical data sets to compare You have 3 or more independent numerical data sets to compare The numerical data is normally distributed. When you need to use a non-parametric test If you have three or more groups, why is it better to use an ANOVA rather than a t test? Choose all that apply. It helps you incorrectly reject the null hypothesis. It's faster and easier to...
Discuss whether women should be allowed to choose a cesarean birth and greater risks over a...
Discuss whether women should be allowed to choose a cesarean birth and greater risks over a vaginal birth when there are no indications for a cesarean birth. Consider cost containment, risk factors, and recovery time. A woman should or should not be forced to have a cesarean birth if she does not want this procedure even though it may be beneficial for the well-being of her fetus.
Choose your favorite unique vector b in R3 with no 0 entries. What is your vector...
Choose your favorite unique vector b in R3 with no 0 entries. What is your vector b? Based on the “Equivalent Conditions for a Non-Singular Matrix” on page 123 in our text, why must Ax = b have a unique solution in this situation? Solve the equation Ax = b the following three ways (you should get the same answer each way!) Recall – calculator OK, in fact suggested. (24 points – 8 points each) Solve the equation Ax =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT