简体   繁体   English

“向量 <bool> MSVC中出现“迭代器不可取消引用”错误,但在使用g ++进行编译时效果最佳

[英]“vector<bool> iterator not dereferencable” error in MSVC but works perfectly when compiled using g++

I was writing sieve of eratosthenes algorithm in MSVC using a vector of bools(since I intended on making the array/vector dynamic with user input) 我正在使用布尔向量在MSVC中编写Eratosthenes算法的筛子(因为我打算通过用户输入使数组/向量动态化)

My code: 我的代码:

#include<iostream>
#include<cmath>
#include<vector>

void sieve(std::vector<bool>& prime)
{
    long long size = prime.size();
    long long sq = (long long)sqrt(size);
    if (size >= 2)
        prime[0] = prime[1] = false;
    for (long long i = 2; i <= sq; ++i)
        if (prime[i])
            for (long long j = i*i; j <= size; j += i)
                prime[j] = false;
}

int main()
{
    int m, n;
    std::cout << "Enter first number: ";
    std::cin >> m;
    std::cout << "Enter second number: ";
    std::cin >> n;
    std::vector<bool> prime(n, true);
    sieve(prime);
    for (long long i = m; i <= n; ++i)
        if (prime[i])
            std::cout << i << std::endl;
}

I stumbled upon a run time error in MSVC 我偶然发现了MSVC中的运行时错误

MSVC Error MSVC错误

But this code works perfectly when compiled using g++. 但是,当使用g ++编译时,此代码可以完美地工作。 I don't know whats wrong. 我不知道怎么了 Any help would be appreciable 任何帮助都将是可观的

Thank you 谢谢

for (long long i = m; i <= n; ++i) and for (long long j = i*i; j <= size; j += i) will both run past the end of the vector as vector_name[vector_size] is 1 past the end of the elements in the vector. for (long long i = m; i <= n; ++i)for (long long j = i*i; j <= size; j += i)都将作为vector_name[vector_size]是向量中元素末尾的1。 This is undefined behavior and you were unlucky that it worked on g++. 这是未定义的行为,您很不幸它无法在g ++上运行。 Some people never bother to try and compile on another compiler to see if they get the same results and If you hadn't there would have been a silent bug in your "working code". 有些人从不费力尝试在其他编译器上进行编译,以查看它们是否获得相同的结果,如果没有,则“工作代码”中将存在一个无声的错误。

Change the loops to for (long long i = m; i < n; ++i) and for (long long j = i*i; j < size; j += i) and you will no longer run past the end of the vector. 将循环更改为for (long long i = m; i < n; ++i)for (long long j = i*i; j < size; j += i) ,您将不再运行到向量。

for (long long j = i*i
  prime[j] = false; 

This is your problem the position is greater than your size of your vector. 这是您的问题,位置大于向量的大小。

Another thing that I have noticed is the size of your vector should be n*n : 我注意到的另一件事是向量的大小应为n*n

std::vector<bool> prime(n*n, true);
void sieve(std::vector<bool>& prime,int m,int n)
{
  long long size = prime.size();
  // long long sq = (long long)sqrt(size); you can use n for this so you don't have to make another variable.
  if (size >= 2)
    prime[0] = prime[1] = false;

  for (long long i = m; i < n; ++i)
  {
    if (prime[i])
    {
      for (long long j = i*i; j < n*n; j += i)
      {
        prime[j] = false;
      }
    }
  }
}
int main()
{
  int m, n;
  std::cout << "Enter first number: ";
  std::cin >> m;
  std::cout << "Enter second number: ";
  std::cin >> n;
  std::vector<bool> prime(n*n, true);
  sieve(prime,m,n);
  for (long long i = m; i <= n; ++i)
  {
    if (prime[i])
    {
      std::cout << i << std::endl;
    }
  }
}

this should work ;), and don`t forget to include the headers. 这应该起作用;),并且不要忘记包含标题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM