Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ SPOJ中的运行时错误(SIGSEGV)_C++ - Fatal编程技术网

C++ SPOJ中的运行时错误(SIGSEGV)

C++ SPOJ中的运行时错误(SIGSEGV),c++,C++,我正在解决这个基于素数生成的简单问题。在Xcode中,它成功运行。但是当我向它提交解决方案时,它说运行时错误SIGSEGV。我在互联网上搜索了这个运行时错误是什么,当我检查我的解决方案时,我没有发现任何问题那么,如果我的代码中有任何问题,那是什么?如何解决? #include <iostream> #include <list> using std::cout; using std::cin; using std::endl; int main() { int t

我正在解决这个基于素数生成的简单问题。在Xcode中,它成功运行。但是当我向它提交解决方案时,它说
运行时错误SIGSEGV
。我在互联网上搜索了这个运行时错误是什么,当我检查我的解决方案时,我没有发现任何问题那么,如果我的代码中有任何问题,那是什么?如何解决?

#include <iostream>
#include <list>
using std::cout;
using std::cin;
using std::endl;

int main() {
   int tcases = 0;
   cin >> tcases;
   const int ccase = tcases;
   tcases = 0;

   while (tcases != ccase) {
     int m = 0, n = 0;
     cin >> m >> n;
     std::list<int> p;
     int i = 0;
     if (m == 1) i = ++m;
     if (m > 1) i = m;
     for (; i <= n; p.push_back(i), ++i);
     // get all the elements
     for (auto first = p.begin(), last = p.end(); first != last; ++first) {
         if (*first == 2) continue;
         else if (*first == 3) continue;
         else if (*first == 5) continue;
         else if (*first == 7) continue;
         else if (*first % 2 == 0) p.erase(first);
         else if (*first % 3 == 0) p.erase(first);
         else if (*first % 5 == 0) p.erase(first);
         else if (*first % 7 == 0) p.erase(first);
     }
     for (auto &elem: p)
         cout << elem << endl;

     cout << endl;
     ++tcases;
    } 
   return 0;
 }
#包括
#包括
使用std::cout;
使用std::cin;
使用std::endl;
int main(){
int tcases=0;
cin>>tcase;
常数int ccase=TCASE;
tcase=0;
while(tcase!=ccase){
int m=0,n=0;
cin>>m>>n;
std::列表p;
int i=0;
如果(m==1)i=++m;
如果(m>1)i=m;

因为问题是当你说

p.erase(first);
Erase删除当前元素并将迭代器返回到列表中的下一个元素,您没有将其存储在代码中的任何位置。并且您正在for循环中递增
first
,因此下次循环运行时,
first未指向有效位置,因此出现运行时错误。

将循环的
更改为以下内容

for (auto first = p.begin(), last = p.end(); first != last;) {
            if (*first == 2)
            {

                first++;
                continue;
            }
            else if (*first == 3)
            {

                first++;
                continue;
            }
            else if (*first == 5) 
            {

                first++;
                continue;
            }
            else if (*first == 7) 
            {
                first++;
                continue;
            }
            else if (*first % 2 == 0) 
                first = p.erase(first);
            else if (*first % 3 == 0) 
            first = p.erase(first);
            else if (*first % 5 == 0) 
            first = p.erase(first);
            else if (*first % 7 == 0)
                first = p.erase(first);
            else
                first++;
        }

备注:共享代码只是为了演示如何使用擦除。

对我来说很有效,通过不同的输入测试,看看你是否正确更改了它。但是你的问题是你使用擦除的方式错误。在我的系统上,它正常工作。但当我提交时,它显示了错误的答案。错误的答案或运行时错误?如果答案错误,你需要c检查你的逻辑。我按照你说的更改了for循环部分。当我在我的系统上运行时,结果会正确显示。但是当我提交代码时,它会显示
错误答案
。我尝试删除错误,我想它对你有效,因为你现在没有得到运行时错误。对于逻辑部分,你需要检查自己:)