Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8361381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:41:10+00:00 2026-06-09T11:41:10+00:00

I was trying one of the project euler problems, the first one where it

  • 0

I was trying one of the project euler problems, the first one where it asked you to calculate the sum of all the multiples of 3 and 5 below 1000.
I attempted it and it shows no errors, however when i run it i get a message box with the error:

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   

here is the code:

#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;
}

I know there is a much easier way to do that problem, however i am still learning c++ and wanted to try and use some of the things i had recently learnt.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:41:11+00:00Added an answer on June 9, 2026 at 11:41 am

    std::vector<T>::erase will return you an iterator following the last removed element. If you remove the last element, the returned iterator will be end(). And then you increment the iterator and get an exception. Also, even if you don’t delete the last entry but another, you will still ignore the following element.

    By the way, what do you want to achieve with five[*it]? The iterator acts like a pointer to a given element in the container. Either use a simple for-loop with an int i and five[i] (which will have the same problems I stated above) or *it.*

    Try the following code instead:

    for(vector<int>::iterator it = five.begin(); it != five.end();)
    {
        if (*it % 3 == 0)
        {
            it = five.erase(it);
        }
        else
             ++it;
    }
    

    * While it’s true that the value of your iterator is its own key this will only last until you first changed the vector. So after your very first erase five[*it] != *it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently trying out Project Euler and one of the question is calculate
I am trying to do problem 12 in Project Euler. numDivisor64 is to calculate
I am trying to solve math problems with Ruby from the Project Euler. Here
I'm trying to solve questions from Project Euler in Ruby one-liners, and I'm curious
I'm trying to learn Python by working through the problems on the Project Euler
I was trying Project Euler and am stuck on this one: A palindromic number
I'm trying to do a Project Euler question. I'm looking for the sum of
I am trying to test some exceptions in my project and one of the
I am trying to use this jQuery plugin (jsTree) in one of my project.
I'm trying to pull a project from git, in particular, this one: https://github.com/pocmo/Yaaic .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.