Visual Studio C++列表迭代器不可递减

发布于 2024-09-04 19:17:22 字数 3979 浏览 1 评论 0原文

我在 Visual Studio 上不断收到错误,显示 list iterator not decrementable: line 256

我的程序在 Linux 上运行良好,但 Visual Studio 编译器会抛出此错误。

无论如何,你明白我的问题是什么吗?

#include <iostream>
#include <fstream>
#include <sstream>
#include <list>

using namespace std;

int main(){

    /** create the list **/
    list<int> l;

    /** create input stream to read file **/
 ifstream inputstream("numbers.txt");

 /** read the numbers and add them to list **/
 if( inputstream.is_open() ){

  string line;
  istringstream instream;
  while( getline(inputstream, line) ){
      instream.clear();
      instream.str(line);

      /** get he five int's **/
      int one, two, three, four, five;
      instream >> one >> two >> three >> four >> five;

      /** add them to the list **/
      l.push_back(one);
      l.push_back(two);
      l.push_back(three);
      l.push_back(four);
      l.push_back(five);
  }//end while loop

 }//end if

 /** close the stream **/
 inputstream.close();

 /** display the list **/
 cout << "List Read:" << endl;
 list<int>::iterator i;
 for( i=l.begin(); i != l.end(); ++i){
     cout << *i << " ";
 }
 cout << endl << endl;

 /** now sort the list **/
 l.sort();

 /** display the list **/
 cout << "Sorted List (head to tail):" << endl;
 for( i=l.begin(); i != l.end(); ++i){
     cout << *i << " ";
 }
 cout << endl;

 list<int> lReversed;
 for(i=l.begin(); i != l.end(); ++i){
     lReversed.push_front(*i);
 }
 cout << "Sorted List (tail to head):" << endl;
 for(i=lReversed.begin(); i!=lReversed.end(); ++i){
     cout << *i << " ";
 }
 cout << endl << endl;

 /** remove first biggest element and display **/
 l.pop_back();
 cout << "List after removing first biggest element:" << endl;
 cout << "Sorted List (head to tail):" << endl;
 for( i=l.begin(); i != l.end(); ++i){
     cout << *i << " ";
 }
 cout << endl;
 cout << "Sorted List (tail to head):" << endl;
    lReversed.pop_front();
    for(i=lReversed.begin(); i!=lReversed.end(); ++i){
     cout << *i << " ";
 }
 cout << endl << endl;

    /** remove second biggest element and display **/
 l.pop_back();
 cout << "List after removing second biggest element:" << endl;
 cout << "Sorted List (head to tail):" << endl;
 for( i=l.begin(); i != l.end(); ++i){
     cout << *i << " ";
 }
    cout << endl;

    lReversed.pop_front();
 cout << "Sorted List (tail to head):" << endl;
 for(i=lReversed.begin(); i!=lReversed.end(); ++i){
     cout << *i << " ";
 }
 cout << endl << endl;

 /** remove third biggest element and display **/
 l.pop_back();
 cout << "List after removing third biggest element:" << endl;
 cout << "Sorted List (head to tail):" << endl;
 for( i=l.begin(); i != l.end(); ++i){
     cout << *i << " ";
 }
    cout << endl;
 cout << "Sorted List (tail to head):" << endl;
 lReversed.pop_front();
 for(i=lReversed.begin(); i!=lReversed.end(); ++i){
     cout << *i << " ";
 }
 cout << endl << endl;

 /** create frequency table **/
 const int biggest = 1000;

 //create array size of biggest element
 int arr[biggest];
 //set everything to zero
 for(int j=0; j<biggest+1; j++){
     arr[j] = 0;
 }

 //now update number of occurences
 for( i=l.begin(); i != l.end(); i++){
     arr[*i]++;
 }

 //now print the frequency table. only print where occurences greater than zero
 cout << "Final list frequency table: " << endl;
 for(int j=0; j<biggest+1; j++){
     if( arr[j] > 0 ){
         cout << j << ": " << arr[j] << " occurences" << endl;
     }
 }




    return 0;
}//end main

I keep getting an error on visual studio that says list iterator not decrementable: line 256

My program works fine on Linux, but the Visual Studio compiler throws this error.

Anyway, do you see what my problem is?

#include <iostream>
#include <fstream>
#include <sstream>
#include <list>

using namespace std;

int main(){

    /** create the list **/
    list<int> l;

    /** create input stream to read file **/
 ifstream inputstream("numbers.txt");

 /** read the numbers and add them to list **/
 if( inputstream.is_open() ){

  string line;
  istringstream instream;
  while( getline(inputstream, line) ){
      instream.clear();
      instream.str(line);

      /** get he five int's **/
      int one, two, three, four, five;
      instream >> one >> two >> three >> four >> five;

      /** add them to the list **/
      l.push_back(one);
      l.push_back(two);
      l.push_back(three);
      l.push_back(four);
      l.push_back(five);
  }//end while loop

 }//end if

 /** close the stream **/
 inputstream.close();

 /** display the list **/
 cout << "List Read:" << endl;
 list<int>::iterator i;
 for( i=l.begin(); i != l.end(); ++i){
     cout << *i << " ";
 }
 cout << endl << endl;

 /** now sort the list **/
 l.sort();

 /** display the list **/
 cout << "Sorted List (head to tail):" << endl;
 for( i=l.begin(); i != l.end(); ++i){
     cout << *i << " ";
 }
 cout << endl;

 list<int> lReversed;
 for(i=l.begin(); i != l.end(); ++i){
     lReversed.push_front(*i);
 }
 cout << "Sorted List (tail to head):" << endl;
 for(i=lReversed.begin(); i!=lReversed.end(); ++i){
     cout << *i << " ";
 }
 cout << endl << endl;

 /** remove first biggest element and display **/
 l.pop_back();
 cout << "List after removing first biggest element:" << endl;
 cout << "Sorted List (head to tail):" << endl;
 for( i=l.begin(); i != l.end(); ++i){
     cout << *i << " ";
 }
 cout << endl;
 cout << "Sorted List (tail to head):" << endl;
    lReversed.pop_front();
    for(i=lReversed.begin(); i!=lReversed.end(); ++i){
     cout << *i << " ";
 }
 cout << endl << endl;

    /** remove second biggest element and display **/
 l.pop_back();
 cout << "List after removing second biggest element:" << endl;
 cout << "Sorted List (head to tail):" << endl;
 for( i=l.begin(); i != l.end(); ++i){
     cout << *i << " ";
 }
    cout << endl;

    lReversed.pop_front();
 cout << "Sorted List (tail to head):" << endl;
 for(i=lReversed.begin(); i!=lReversed.end(); ++i){
     cout << *i << " ";
 }
 cout << endl << endl;

 /** remove third biggest element and display **/
 l.pop_back();
 cout << "List after removing third biggest element:" << endl;
 cout << "Sorted List (head to tail):" << endl;
 for( i=l.begin(); i != l.end(); ++i){
     cout << *i << " ";
 }
    cout << endl;
 cout << "Sorted List (tail to head):" << endl;
 lReversed.pop_front();
 for(i=lReversed.begin(); i!=lReversed.end(); ++i){
     cout << *i << " ";
 }
 cout << endl << endl;

 /** create frequency table **/
 const int biggest = 1000;

 //create array size of biggest element
 int arr[biggest];
 //set everything to zero
 for(int j=0; j<biggest+1; j++){
     arr[j] = 0;
 }

 //now update number of occurences
 for( i=l.begin(); i != l.end(); i++){
     arr[*i]++;
 }

 //now print the frequency table. only print where occurences greater than zero
 cout << "Final list frequency table: " << endl;
 for(int j=0; j<biggest+1; j++){
     if( arr[j] > 0 ){
         cout << j << ": " << arr[j] << " occurences" << endl;
     }
 }




    return 0;
}//end main

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静赏你的温柔 2024-09-11 19:17:22

你写的 arr 数组越界了,在 VS2008 中使用调试配置编译后运行它时会导致堆栈损坏,甚至会告诉你弄乱了哪些变量。

问题在于,您尝试写入数组的长度,而不是长度 - 1。您不仅尝试写入越界,而且稍后还会读取它。(第 122、133 和 134 行)

没有收到编译器错误或警告,也没有调试断言告诉我,当我运行程序时,我正在尝试在任何地方减少不可递减的迭代器。您看到的错误可能只是堆栈损坏的副作用,但我只是猜测。

You're writing out of bounds of your arr array, it causes stack corruption when running it after compiling with the debugging config in VS2008, even tells you what variables you messed up with.

The problem lies in the fact that you try to write to the length of the array, instead of the length - 1. Not only do you do try writing out of bounds, you also read it later.(Line 122, 133 and 134)

Not getting compiler errors or warnings, nor a debug assertion telling me that I'm trying to decrement a non-decrementable iterator anywhere when I run the program. The error you're seeing could just be a side effect from corrupting the stack, but I'm just guessing there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文