Skip to main content
Warn on potential issues with the answer
Source Link
Pierre Baret
  • 1.9k
  • 2
  • 20
  • 37

Note also that processing the vector in reverse order, you CAN'T use unsigned types for indices (for (uint8_t i = v.size() -1; ... won't work). This because when i equals 0, i-- will overflow and be equal to 255 for uint8_t for example (so the loop won't stop as i will still be >= 0, and probably out of bounds of the vector).

Note also that processing the vector in reverse order, you CAN'T use unsigned types for indices (for (uint8_t i = v.size() -1; ... won't work). This because when i equals 0, i-- will overflow and be equal to 255 for uint8_t for example (so the loop won't stop as i will still be >= 0, and probably out of bounds of the vector).

deleted 12 characters in body
Source Link
Pierre Baret
  • 1.9k
  • 2
  • 20
  • 37

for (size_tint i = v.size() - 1; i >= 0; i--)

for (size_tint i = 0; i < v.size(); i++)

#include <iostream> #include <vector> using namespace std; void printVector(const vector<int> &v) { for (size_t i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; } int main() { vector<int> v1, v2; for (int i = 0; i < 10; i++) { v1.push_back(i); v2.push_back(i); } // print v1 cout << "v1: " << endl; printVector(v1); cout << endl; // print v2 cout << "v2: " << endl; printVector(v2); // Erase all odd elements cout << "--- Erase odd elements ---" << endl; // loop with decreasing indices cout << "Process v2 with decreasing indices: " << endl; for (size_tint i = v2.size() - 1; i >= 0; i--) { if (v2[i] % 2 != 0) { cout << "# "; v2.erase(v2.begin() + i); } else { cout << v2[i] << " "; } } cout << endl; cout << endl; // loop with increasing indices cout << "Process v1 with increasing indices: " << endl; for (size_tint i = 0; i < v1.size(); i++) { if (v1[i] % 2 != 0) { cout << "# "; v1.erase(v1.begin() + i); } else { cout << v1[i] << " "; } } return 0; } 

for (size_t i = v.size() - 1; i >= 0; i--)

for (size_t i = 0; i < v.size(); i++)

#include <iostream> #include <vector> using namespace std; void printVector(const vector<int> &v) { for (size_t i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; } int main() { vector<int> v1, v2; for (int i = 0; i < 10; i++) { v1.push_back(i); v2.push_back(i); } // print v1 cout << "v1: " << endl; printVector(v1); cout << endl; // print v2 cout << "v2: " << endl; printVector(v2); // Erase all odd elements cout << "--- Erase odd elements ---" << endl; // loop with decreasing indices cout << "Process v2 with decreasing indices: " << endl; for (size_t i = v2.size() - 1; i >= 0; i--) { if (v2[i] % 2 != 0) { cout << "# "; v2.erase(v2.begin() + i); } else { cout << v2[i] << " "; } } cout << endl; cout << endl; // loop with increasing indices cout << "Process v1 with increasing indices: " << endl; for (size_t i = 0; i < v1.size(); i++) { if (v1[i] % 2 != 0) { cout << "# "; v1.erase(v1.begin() + i); } else { cout << v1[i] << " "; } } return 0; } 

for (int i = v.size() - 1; i >= 0; i--)

for (int i = 0; i < v.size(); i++)

#include <iostream> #include <vector> using namespace std; void printVector(const vector<int> &v) { for (size_t i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; } int main() { vector<int> v1, v2; for (int i = 0; i < 10; i++) { v1.push_back(i); v2.push_back(i); } // print v1 cout << "v1: " << endl; printVector(v1); cout << endl; // print v2 cout << "v2: " << endl; printVector(v2); // Erase all odd elements cout << "--- Erase odd elements ---" << endl; // loop with decreasing indices cout << "Process v2 with decreasing indices: " << endl; for (int i = v2.size() - 1; i >= 0; i--) { if (v2[i] % 2 != 0) { cout << "# "; v2.erase(v2.begin() + i); } else { cout << v2[i] << " "; } } cout << endl; cout << endl; // loop with increasing indices cout << "Process v1 with increasing indices: " << endl; for (int i = 0; i < v1.size(); i++) { if (v1[i] % 2 != 0) { cout << "# "; v1.erase(v1.begin() + i); } else { cout << v1[i] << " "; } } return 0; } 
Source Link
Pierre Baret
  • 1.9k
  • 2
  • 20
  • 37

It may seem obvious to some people, but to elaborate on the above answers:

If you are doing removal of std::vector elements using erase in a loop over the whole vector, you should process your vector in reverse order, that is to say using

for (size_t i = v.size() - 1; i >= 0; i--)

instead of (the classical)

for (size_t i = 0; i < v.size(); i++)

The reason is that indices are affected by erase so if you remove the 4-th element, then the former 5-th element is now the new 4-th element, and it won't be processed by your loop if you're doing i++.

Below is a simple example illustrating this where I want to remove all the odds element of an int vector;

#include <iostream> #include <vector> using namespace std; void printVector(const vector<int> &v) { for (size_t i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; } int main() { vector<int> v1, v2; for (int i = 0; i < 10; i++) { v1.push_back(i); v2.push_back(i); } // print v1 cout << "v1: " << endl; printVector(v1); cout << endl; // print v2 cout << "v2: " << endl; printVector(v2); // Erase all odd elements cout << "--- Erase odd elements ---" << endl; // loop with decreasing indices cout << "Process v2 with decreasing indices: " << endl; for (size_t i = v2.size() - 1; i >= 0; i--) { if (v2[i] % 2 != 0) { cout << "# "; v2.erase(v2.begin() + i); } else { cout << v2[i] << " "; } } cout << endl; cout << endl; // loop with increasing indices cout << "Process v1 with increasing indices: " << endl; for (size_t i = 0; i < v1.size(); i++) { if (v1[i] % 2 != 0) { cout << "# "; v1.erase(v1.begin() + i); } else { cout << v1[i] << " "; } } return 0; } 

Output:

v1: 0 1 2 3 4 5 6 7 8 9 v2: 0 1 2 3 4 5 6 7 8 9 --- Erase odd elements --- Process v2 with decreasing indices: # 8 # 6 # 4 # 2 # 0 Process v1 with increasing indices: 0 # # # # # 

Note that on the second version with increasing indices, even numbers are not displayed as they are skipped because of i++