I was asked to create a program to find word frequencies, word length in a text file. I am able to use map to do the frequencies part but don't know how to sort the the output accordingly to the length of the words. The example output that I should get is
1 a. 362
i. 157
2 an. 122
at. 201
3 add. 2
age. 1
int main(void)
{
static const char* file = "demo.txt";
map<string, unsigned int> wcount;
{
ifstream fileStream(file);
if (fileStream.is_open())
while (fileStream.good())
{string word;
fileStream >> word;
if (wcount.find(word) == wcount.end())
wcount[word] = 1;
else
wcount[word]++;
}
else
{
cerr << "Unable to open file." << endl;
return EXIT_FAILURE;
}
display(wcount);
}
return EXIT_SUCCESS;
}