C++ 当使用向量大小作为while循环的标准时,如何防止调试ERORR?
我创建了一个程序,可以将2到100之间的所有整数的向量(numberset)转换为该向量中的素数列表。它是通过从向量中移除数字来实现的,如果这些数字可以被除自身之外的一系列数字整除 我很难让这段代码在没有调试错误的情况下运行,最终确定问题与使用向量的大小作为while循环条件的一部分有关。因为从向量中删除数字也会减少向量的大小,所以我的循环变量(j)可能会“跳过”向量中的条目——可能会导致它尝试访问不存在的向量大小 我找到的解决方案是,每当我减少向量大小时,将循环变量减少1。然而,我想知道是否有其他方法可以解决这个问题。谢谢大家!C++ 当使用向量大小作为while循环的标准时,如何防止调试ERORR?,c++,vector,while-loop,C++,Vector,While Loop,我创建了一个程序,可以将2到100之间的所有整数的向量(numberset)转换为该向量中的素数列表。它是通过从向量中移除数字来实现的,如果这些数字可以被除自身之外的一系列数字整除 我很难让这段代码在没有调试错误的情况下运行,最终确定问题与使用向量的大小作为while循环条件的一部分有关。因为从向量中删除数字也会减少向量的大小,所以我的循环变量(j)可能会“跳过”向量中的条目——可能会导致它尝试访问不存在的向量大小 我找到的解决方案是,每当我减少向量大小时,将循环变量减少1。然而,我想知道是否有
#include <iostream>
#include "../../std_lib_facilities.h" // available at http://www.stroustrup.com/Programming/std_lib_facilities.h
#include <vector>
vector<int> numberset;
int j = 0;
int main()
{
bool primetest = true;
// Adding numbers to our list of potential primes (excluding the number 1)
for (int i = 2; i <= 100; i++)
{
numberset.push_back(i);
}
// j represents the current position within the vector numberset
while (j <= numberset.size()-1)
// We're going to look through all the numbers in the vector above, and then divide them by a set of divisors to verify that they're prime. Numbers in the above set that are divisible by a divisor will be removed, since they are not prime.
{
for (int divisor = 2; divisor <= 100; divisor++)
// These are the numbers by which we will divide the vectors
{
//If a certain number within the vector (marked by position j) can be divided a different number >= 2 with a remainder of 0, we know it's not prime.
//We will then set the 'primetest' bool to "false" (indicating that we know this is not a prime number)
if (numberset[j] % divisor == 0 && numberset[j] != divisor)
{
primetest = false;
if (primetest == false)
{
//This line removes the non-prime number from our list of numbers.
numberset.erase(numberset.begin() + j);
//j-- is added in to prevent the code from 'skipping' over numbers in the numberset vector after a number within the vector is removed.
//For example, let's say the third element in the vector (numberset[3]) is 6. If we remove 6 (since it's not prime), the third element then becomes 7, and the 4th element becomes 8.
//However, the j++ below will cause us to next review (numberset[4]), which is 8 rather than 7. Thus the addition of j--, which cancels out the j++ and makes sure we're not skipping elements.
//j++ only runs if a number is erased from the vector, however.
j--;
}
}
}
j++;
}
//This prints out the remaining prime numbers in the vector.
for (int x : numberset)
{
cout << x << " ";
}
}
#包括
#包括“./../std_lib_facilities.h”//可在http://www.stroustrup.com/Programming/std_lib_facilities.h
#包括
向量集;
int j=0;
int main()
{
bool-primetest=true;
//向我们的潜在素数列表中添加数字(不包括数字1)
对于(int i=2;i)您会得到什么运行时错误?而(j)在您将其切换为size\u类型之后,请注意,这是一个无符号数字,如果您尝试从0中减去1,它将下溢。不确定这会如何更改您的算法(我假设这是您现在的问题),但您需要注意。我通过不改变我正在迭代的容器(而是使用单独的结果容器,并且可能在完成时与原始容器交换std::swap
)或者使用@RemyLebeau关于不需要if(primetest==false)的优点来解决这类问题条件;我删除了它。我发现将numberset.size()-1更改为numberset.size()会导致调试错误。如果没有-1,代码不会尝试访问向量中超出向量范围的元素吗?用break替换j--会导致代码生成2加上从3到99的所有奇数。