To insert data into the Forgotten list, well understand, allocate a new node space, add to the tail of the linked list; for adding data to the Vectorz, you need to determine whether the current control is adequate, if not enough, you need to allocate slightly larger space, and then copy the existing elements to the new space area, And then release the space you already have. Perhaps some people think that this copy operation is time-consuming, feel no list to add speed, however, the fact is not so ...
#include <iostream>
#include <time.h>
#include <list>
#include <vector>
using namespace std;
Const LONG num = 1000 * 1000 * 100; Add data number
//
void Test_vector_list (long n) {
vector<int> num1;
List<int> num2;
clock_t Num1_start = Clock ();
for (int i=0;i<n;i++) {
num1.push_back (i);
}
clock_t num1_end = Clock ();
cout<< "Running time vector:" <<static_cast<double> (Num1_end-num1_start)/clocks_per_sec * 1000 < < "MS" <<endl;
clock_t Num2_start = Clock ();
for (int i=0;i<n;i++) {
num2.push_back (i);
}
clock_t num2_end = Clock ();
cout<< "Running Time list:" <<static_cast<double> (Num2_end-num2_start)/clocks_per_sec * 1000 < < "MS" <<endl;
}
int main (int argc, char** argv) {
test_vector_list (num);
return 0;
}