【问题标题】:Visual Studio C++ list iterator not decementableVisual Studio C++ 列表迭代器不可破坏
【发布时间】:2010-06-08 22:37:32
【问题描述】:

我在 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

【问题讨论】:

  • 第 256 行?该文件只有 137 行...
  • 在寻求帮助之前至少 尝试 删除这 100 行中不相关的内容怎么样?
  • @Justin - 我想你的意思是这个函数
  • 我喜欢你如何使用 Visual Studio 提供的有用调试实用程序 并用它来争论 Windows 很糟糕。你必须被误导到什么程度? “嗯,这个 IDE 可以帮助我找到我的代码包含未定义行为的位置,该死的,只是让我有未定义的行为!”
  • 我敢打赌它是 Visual Studio 6。那个编译器根本不支持 STL。也没有 container.clear() 方法。

标签: c++ list stl iterator


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2011-04-13
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    • 1970-01-01
    • 1970-01-01
    • 2019-07-05
    相关资源
    最近更新 更多