0
#include <iostream>
#include <string>
#include <fstream>


using namespace std;


int main()
{
    setlocale(LC_ALL, "rus");
    string text;
    ifstream file("d:\\1\\1.txt");

    int count[256]={0};
    unsigned char a;
    while (!file.eof())
    {
        file >> a;
        count[a]++;// сосчитали букву
    }
for(int i=0;i<256;i++)
   if(count[i]!=0)
     cout<<"Буква "<<(unsigned char)i<<" - "<<count[i]<<" раз\n";
    return 0;
}
Mikhajlo
  • 12,592

1 Answers1

2

Как-то так:

int main()
{
    // setlocale(LC_ALL, "rus");
    string text;
    ifstream file("d:\\1\\1.txt");

    pair<unsigned char,int> count[256];
    for(int i = 0; i < 256; ++i) {
        count[i].first = (unsigned char)i;
        count[i].second = 0;
    }
    unsigned char a;
    while (file >> a)
        count[a].second++;// сосчитали букву

    sort(count, count+256,
         [](auto p, auto q) { return p.second > q.second; });

    for(int i = 0; i<256; i++)
        if (count[i].second != 0)
            cout<<"Буква "<< count[i].first <<" - "<<count[i].second<<" раз\n";
}

Услуга за услугу - откуда у вас это неверное решение:

while (!file.eof())
{

? Ощущение - по количеству пишущих этот, гм... нехороший код - что какой-то чудак его в учебник запихнул?...

Harry
  • 221,325