2010-06-08 41 views
-5

我不斷收到關於Visual Studio中說list iterator not decrementable: line 256Visual Studio的C++列表迭代器不decementable

我的程序工作正常,在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 
+3

+1可查詢 – 2010-06-08 22:40:13

+2

256行?該文件只有137行... – 2010-06-08 22:42:13

+1

至少如何*嘗試*在尋求幫助之前刪除這些100行的不相關的東西? – doublep 2010-06-08 22:42:34

回答

3

你寫出你的ARR數組的邊界,它會導致堆棧損壞與VS2008調試配置編譯後運行它時,甚至會告訴你你搞砸了哪些變量。

問題在於你試圖寫入數組的長度而不是長度 - 1.你不僅試圖寫出越界,你還會在以後閱讀(第122行, 133和134)

沒有收到編譯器錯誤或警告,也沒有調試斷言告訴我,我試圖在運行程序的任何地方減少不可遞減的迭代器。你看到的錯誤可能只是損壞堆棧的副作用,但我只是在那裏猜測。