Home / Expert Answers / Computer Science / c-cs1chw11b-unlike-list-and-deque-vector-does-not-have-a-push-front-funtion-however-vect-pa260

(Solved): C++ cs1CHW11b Unlike list and deque, vector does not have a push_front() funtion. However, vect ...



C++

student submitted image, transcription available below

student submitted image, transcription available below

Unlike list and deque, vector does not have a push_front() funtion. However, vector does have an insert() function that can be used to insert a value at the front ( index [0] ) But remember that before a value can be inserted at the front of a vector, all values must be moved 'down' one to make room for the new value to be inserted at index [0]. This is a very significant processing penalty, to move all values, just to insert one value at the front. It would seem logical that a list would perform best. When a value is inserted at the front of a list, no values in the list are moved. That is also true with a deque, but deques have additional processing related to keeping track of pointers and periodically creating a new chunk. However, each time a value is inserted at the front of a vector, all array values must be moved. So a vector should perform poorly compared to a list or deque. Write the following program and see the results. Write a program that compares the runtimes of these: - vector insert() - list push_front() - deque push_front() - Using a for loop, insert integers into the front of a vector. - Use a timer to measure the time. - Then use a for loop and the push_front() function to push integers into a list. - Use a timer to measure the time. - Then use a for loop and the push_front() function to push 300,000 integers into a deque. - Use a timer to measure the time. /* ---- OUTPUT ---------------- list time: seconds. vector time: seconds deque time: seconds */ // Comparing runtimes - vector insert() vs list push_front() // vs deque push_front () values \#include \#include \#include \#include \#include \#include using namespace std; int main() \{ clock_t start; clock end; list nt numbers ; vectorvalues; dequeamounts; start clock (); for (int ) \{ numbers.push_front(i); \} end ; cout "list time: " (end - start) / CLK_TCK " seconds. ; start for (int ; ) \{ values.insert(values.begin(), i); \} end ; cout "vector time: " (end - start) / CLK_TCK " seconds. ; start for (int ; ) \{ amounts.push_front(i); \} end ; cout "deque time: " (end - start) / CLK_TCK " seconds. "; return 0 ; \} 1 * OUTPUT : list time: 0 seconds. vector time: 7 seconds. deque time: 0 seconds. Press any key to continue... . */


We have an Answer from Expert

View Expert Answer

Expert Answer



We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe