How to fix the error 'map iterator narasimhaswamy'?

To write a program that reads text from a file and outputs to another file the words of this text, placing the words in descending order of their frequency of occurrence. In addition, for each word you want to print the number of occurrences in the source file.

void WordsAnalysisInText(char* path)
 std::map<std::string, int> words;

 // Open text file for analysis
 std::ifstream ifs(path, std::ifstream::in);

 if (!ifs)
 std::cout << "Error!";

 // Create file with result of this function
 int buff_size = ((strchr(path, '\\')) ? (strlen(strchr(path, '\\') + 1)) : strlen(path)) + strlen("Words_analysis_") + 1;
 char* wa_file_name = new char[buff_size];
 strcpy_s(wa_file_name, buff_size, "Words_analysis_");
 if (strchr(path, '\\'))
 strcat_s(wa_file_name, buff_size, strchr(path, '\\') + 1);
 strcat_s(wa_file_name, buff_size, path);

 std::ofstream ofs(wa_file_name, std::ofstream::binary);

 delete[] wa_file_name;

 if (!ofs)
 std::cout << "Error!";

 // Text analysis
 buff_size = 64;
 char* word = new char[buff_size];
 std::string word_str;
 char punctuation_chars[] = "(),.!?;:'\"";

 while (!ifs.the eof())
 ifs >> word; // Reading words

 word_str = word;

 // Remove punctuation
 for (int i = 0; i < strlen(punctuation_chars); ++i)
 word_str.erase(std::remove(word_str.begin(), word_str.end(), punctuation_chars[i]), word_str.end());

 // If the read word is already in <map>
 if ((!words.empty() && words.find(word) != words.end() && word_str != words.end()->first) ||
 std::string(word) == words.end()->first)
 //Add the word in <map>
 words.insert({ word_str, 1 });


 std::map<int, std::string> sorted_words;

 // Sort <map> words
 for (auto& word : words)
 sorted_words.insert({ word.second , word.first });

 for (auto& word : sorted_words)
 ofs << word.second << '' << word.first << '\n';

 delete[] word;

The error text:
map iterator not dereferencable.

I understand this through what I have the key is a string. How to fix this error without breaking the logic of the program? (first time use map)
July 2nd 19 at 14:15
1 answer
July 2nd 19 at 14:17
if ((!words.empty() && words.find(word) != words.end() && word_str != words.end()->first) || std::string(word) == words.end()->first)

The condition is terrible, withdraw to write. Divide into explanatory variables and batch verification equation.

And the problem in words.end()->first.
The iterator returned by end() cannot be dereferenced.

The problem, as I see it, sobesedovaniya. It has a simpler and faster solution with less number of rows.
Yes this one with 5 points, a homework flow. Map and string is still taught, but there's no time to reinvent the wheel. - Horace.Legros75 commented on July 2nd 19 at 14:20
Thanks for the reply. I thought words.end() returns the last element. - Horace.Legros75 commented on July 2nd 19 at 14:23
And, homework. Then all the rules. :)
Look, if you have questions on the standard types, you can always ask the resource . Mine is the best (by the criteria of ease of use, informativeness and credibility) a treatise of the standard language.

Part of the bike: try to invent and imagine a tree in which the word is some path from the root of the tree strictly to the node in the tree. And the node is already stored frequency and other characteristics. Known you map container there will be indispensable.
To sort the frequencies on the fly binary will help a lot . Apply it should be just in the right place. - Benjamin_McClure commented on July 2nd 19 at 14:26
: end returns (sort of) a pointer to the element in the last. - charity84 commented on July 2nd 19 at 14:29
: thank you for useful information) - Horace.Legros75 commented on July 2nd 19 at 14:32

Find more questions by tags C++