Debug Assertion Fail

Hi, i am trying to write a program which calculates the sum of the prime numbers below a specific limit using an algorithm called the Sieve of Eratosthenes(Wikipedia it). I am getting an error though when i run my program. It says debug assertion fail and something how the iterator are incompatible. I have worked much with vectors before, any help??

Here is my code:

#include<iostream>
#include<vector>
#include <numeric>

using namespace std;

int main(){

unsigned int j = 10;
vector<unsigned int> NumList;
for (int i=3; i<=j; i+=2) NumList.push_back(i); // populating vector with odd numbers

vector<unsigned int>::iterator it; // creating an iterator to access values in the vector
vector<unsigned int>::iterator p;
unsigned int q = 0;

while(p<NumList.end()){

p = NumList.begin() + q; // sets p to the first number in the vector which is left and has not been tested

for (it = NumList.begin()+1; it < NumList.end(); it++) {

if(*it%*p == 0) NumList.erase(it); // erases numbers of the vector which are multiples of p

}
q++; // interating q
}

cout << std::accumulate( NumList.begin(), NumList.end(), 0 ) << endl; // summing final values left in the array

system("pause");
return 0;
}
I think you should re-initialize your iterators it and p after you call erase.
Before entering into the loop first initialize the value of p as follows:
p=NumList.begin();
Ok, i tried both the suggestions, but i still get an error about the vector iterators being inconsistent? Could someone explain what this means?
Topic archived. No new replies allowed.