Visual Studio C ++ итератор списка не допускает - PullRequest
0 голосов
/ 09 июня 2010

Я все время получаю сообщение об ошибке в 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

1 Ответ

3 голосов
/ 09 июня 2010

Вы записываете за пределы массива arr, это вызывает повреждение стека при его запуске после компиляции с конфигурацией отладки в VS2008, даже сообщает вам, с какими переменными вы облажались.

Проблема заключается в том, что вы пытаетесь записать в длину массива, а не в длину - 1. Вы не только пытаетесь писать вне границ, но и читаете это позже (Строка 122, 133 и 134)

Не получаю ошибок или предупреждений компилятора, а также отладочного утверждения, говорящего мне, что я пытаюсь уменьшить нерасщепляемый итератор где-нибудь, когда я запускаю программу. Ошибка, которую вы видите, может быть просто побочным эффектом от повреждения стека, но я просто догадываюсь.

...