terminate called after throwing an instance of 'std::logic_error' what<>: basic_string::_M_construct null not valid

0

terminate called after throwing an instance of 'std::logic_error' what<>: basic_string::_M_construct null not valid


#include <iostream>
#include <sstream>
#include <string>
#include <map>

using namespace std;

int main (int argc,char *argv [])
{
    int length;
    istringstream (argv [1])>>length;
    string line;
    getline (cin,line);
    map <string,int> ocurrences;
    for (int index=0; index!=line.size ()-length+1; ++index)
        ++ocurrences [line.substr (index,length)];
    map <string,int>::iterator max=ocurrences.begin ();
    for (map <string,int>::iterator iter=ocurrences.begin ();
            iter!=ocurrences.end ();
            ++iter)
        if (iter->second>max->second)
            max=iter;
    cout<<max->first<<endl;
    return 0;
}


Otrzymuję taki oto błąd, znalazłam  podobne wątki jednak błędy dotyczyły inicjalizacji zmiennej = 0, 

wydaje mi się że  błąd jest tej linijce kodu, 
dziękuje za pomoc



istringstream (argv [1])>>length;

wydaje mi się że błąd jest tej linijce kodu,
dziękuje za pomoc

2

A co Ci mówi debugger, uruchamiałeś go?
Jakie argumenty przekazujesz do programu? Prawdopodobnie żadnych i stąd problem.

2

Dopisz coś takiego:

int main (int argc,char *argv [])
{
   if (argc < 2) {
       std::cerr << "Nie podano nazwy pliku wejściowego!\n";
       return 1;
   }

1 użytkowników online, w tym zalogowanych: 0, gości: 1