Skip to main content
Copy edited.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

If you work with large vectors (size > 100000100,000) and want to delete lots of elements, I would recommend to do something like this:

int main(int argc, char** argv) {   vector <int> vec;   vector <int> vec2;   for (int i = 0; i < 20000000; i++){   vec.push_back(i);}   for (int i = 0; i < vec.size(); i++)   {   if(vec.at(i) %3 != 0)   vec2.push_back(i);   }   vec=vec2; vec = vec2;   cout << vec.size() << endl; }  

The code takes every number in vec, that can't be divided by 3 and copies it to vec2. Afterwards it copies vec2 inin vec. It It is pretty fast. To proceedprocess 20.,000.,000 elements this algorithm only takes 0.8 sec!

I did the same thing with the erase-method, and it takes lots and lots of time:

Erase-Version (10k elements) : 0.04 sec Erase-Version (100k elements) : 0.6 sec Erase-Version (1000k elements): 56 sec Erase-Version (10000k elements): ...still calculating (>30min>30 min) 

If you work with large vectors (size > 100000) and want to delete lots of elements, I would recommend to do something like this:

int main(int argc, char** argv) { vector <int> vec; vector <int> vec2; for (int i = 0; i < 20000000; i++){ vec.push_back(i);} for (int i = 0; i < vec.size(); i++) { if(vec.at(i) %3 != 0) vec2.push_back(i); } vec=vec2; cout << vec.size() << endl; }  

The code takes every number in vec, that can't be divided by 3 and copies it to vec2. Afterwards it copies vec2 in vec. It is pretty fast. To proceed 20.000.000 elements this algorithm only takes 0.8 sec!

I did the same thing with the erase-method and it takes lots and lots of time:

Erase-Version (10k elements) : 0.04 sec Erase-Version (100k elements) : 0.6 sec Erase-Version (1000k elements): 56 sec Erase-Version (10000k elements): ...still calculating (>30min) 

If you work with large vectors (size > 100,000) and want to delete lots of elements, I would recommend to do something like this:

int main(int argc, char** argv) {   vector <int> vec;   vector <int> vec2;   for (int i = 0; i < 20000000; i++){   vec.push_back(i);}   for (int i = 0; i < vec.size(); i++)   {   if(vec.at(i) %3 != 0)   vec2.push_back(i);   }    vec = vec2;   cout << vec.size() << endl; } 

The code takes every number in vec that can't be divided by 3 and copies it to vec2. Afterwards it copies vec2 in vec. It is pretty fast. To process 20,000,000 elements this algorithm only takes 0.8 sec!

I did the same thing with the erase-method, and it takes lots and lots of time:

Erase-Version (10k elements) : 0.04 sec Erase-Version (100k elements) : 0.6 sec Erase-Version (1000k elements): 56 sec Erase-Version (10000k elements): ...still calculating (>30 min) 
Source Link
Fabian
  • 129
  • 1
  • 4

If you work with large vectors (size > 100000) and want to delete lots of elements, I would recommend to do something like this:

int main(int argc, char** argv) { vector <int> vec; vector <int> vec2; for (int i = 0; i < 20000000; i++){ vec.push_back(i);} for (int i = 0; i < vec.size(); i++) { if(vec.at(i) %3 != 0) vec2.push_back(i); } vec=vec2; cout << vec.size() << endl; } 

The code takes every number in vec, that can't be divided by 3 and copies it to vec2. Afterwards it copies vec2 in vec. It is pretty fast. To proceed 20.000.000 elements this algorithm only takes 0.8 sec!

I did the same thing with the erase-method and it takes lots and lots of time:

Erase-Version (10k elements) : 0.04 sec Erase-Version (100k elements) : 0.6 sec Erase-Version (1000k elements): 56 sec Erase-Version (10000k elements): ...still calculating (>30min)