저는 프로젝트 오일러 문제 중 하나를 시도했습니다. 첫 번째 문제는 1000 이하의 3과 5의 배수를 모두 계산하도록 요청한 것입니다. 시도했는데 아무 것도 표시하지 않습니다. 내가 그것을 실행할 때 오류가 그러나 나는 오류 메시지 상자를 얻을 : 그러나, 나는 그 문제를 할 수있는 더 쉬운 방법이 알고벡터 아래 첨자가 범위를 벗어났습니다. 오류 메시지
#include <iostream>
#include <vector>
#include <numeric>
using std::endl; using std::cout;
using std::vector;
int main()
{
vector<int> five;
vector<int> three;
int x;
int y;
int sum;
for(int i = 0; i < 1000; i = i + 5)
{
five.push_back(i);
}
for(int i = 0; i < 1000; i = i + 3)
{
three.push_back(i);
}
for(vector<int>::iterator it = five.begin(); it != five.end(); ++it)
{
if (five[*it] % 3 == 0)
{
it = five.erase(it);
}
}
for(vector<int>::iterator it = three.begin(); it != three.end(); ++it)
{
if (three[*it] % 5 == 0)
{
it = three.erase(it);
}
}
x = accumulate(five.begin(), five.end(), 0);
cout << x << endl;
y = accumulate(three.begin(), three.end(), 0);
cout << y << endl;
sum = x + y;
cout << sum << endl;
system("PAUSE");
return 0;
}
: 여기
Microsoft Visual C++ Debug Library
Debug Assertion Failed!
Program: ...\c++ learning\project euler ex 1\Debug\project euler ex 1.exe
File: c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\vector
Line: 932
Expression: vector subscript out of range
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
Abort Retry Ignore
코드입니다 여전히 C++을 배우고 있으며 최근에 배웠던 것들 중 일부를 사용해보고 싶었습니다. 티.
시작이 아니라 벡터의 끝에서 반복 시작합니다. – user15
반복하는 컬렉션을 수정하지 마십시오. –