There is here a small program, which in theory should display the most frequently entered name and the number of its repetitions. But when doing it, it just crashes. Please help.

int main(int argc, char** argv) { russian(); //отдельная функция в заголовочном файле, не обращайте внимания vector<string> strings; string current = " "; string max = " "; int nmax = 0; int count = 0; cout << "Введите последовательность имён "; while(cin >> current) { strings.push_back(current); } sort(strings.begin(), strings.end()); current = " "; for(int i = 0; i < strings.size(); ++i){ if(current == strings[i]) { ++count; } else { if(count >= nmax) { nmax = count; max = strings[i - 1]; } else { current = strings[i]; count = 0; } } } cout << "\nКоличество повторений: " << nmax << "\nИмя повторяющееся наибольшее количество раз: " << max; return 0; } 

Closed due to the fact that the participants are off topic AK , αλεχολυτ , aleksandr barakin , user194374, pavel Jan 8 '17 at 8:15 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - aleksandr barakin, community spirit, pavel
If the question can be reformulated according to the rules set out in the certificate , edit it .

    2 answers 2

    As it was already correctly noted, at the very first iteration of the cycle, when the variable i is equal to 0, the non-existing element of the vector is addressed

     max = strings[i - 1] ^^^^^^ 

    You can simplify the logic of the program. The following shows how to do this using a nested loop.

     #include <iostream> #include <vector> #include <string> #include <algorithm> int main() { std::vector<std::string> v; std::string s; while (std::cin >> s) v.push_back(s); std::sort(v.begin(), v.end()); size_t max_count = 0; s.clear(); for ( size_t i = 0; i < v.size(); ) { size_t j = i; while (i < v.size() && v[i] == v[j]) i++; if (max_count < i - j) { max_count = i - j; s = v[j]; } } std::cout << s << ": " << max_count << std::endl; return 0; } 

    The program would be even more transparent if, instead of a vector, the standard container std :: map is used, which already includes the elements in an orderly manner.

    For example,

     #include <iostream> #include <map> #include <string> #include <algorithm> int main() { std::map<std::string, size_t> m; std::string s; while (std::cin >> s) ++m[s]; auto it = std::max_element( m.begin(), m.end(), []( const auto &max, const auto &current ) { return max.second < current.second; }); std::cout << it->first << ": " << it->second << std::endl; return 0; } 

      You have a problem at 1 iteration.

      Let i = 0

       if(count >= nmax) { nmax = count; max = strings[i - 1]; ^^^^^^ } 

      count = 0, nmax = 0 . The code will be executed. Well, going beyond the array. The easiest way is to replace the >= sign with > .