tags:

views:

12

answers:

1

Vector v; int i=0; while(i!=999) { cin>>i; v.push_back(i); }

Time taken by this piece of code could vary when the number of inputs vary. Since vector would take amortized time for new allocation. Even for same size the program at different times could take different time.

Suggest changes (e.g. use list instead of vector), which makes the time a function of number of inputs.

A: 

In the current version using vector, time is already a function of the number of inputs, and how long it takes the user to enter each value.

In this case, the I/O done by cin will be so much more expensive than adding the item to any sort of container, that the container type will almost certainly not even matter. Instead of trying to optimize the container type for the insertion, instead consider which container provides the properties that you actually need.

Mark B