Quantcast
Channel: Dạy Nhau Học - Latest topics
Viewing all articles
Browse latest Browse all 34346

Nhờ giải thích đoạn code

$
0
0

@sincos wrote:

e có đoạn code này

#include <iostream>
#include <map>
#include <fstream>
using namespace std;

string st;
map <char, int> xh;

int main()
{
    cin >> st;

    ifstream file(st);
    if(!file) return 0;

    std::map<char, int >::iterator it;
    string s;
    while( getline(file, s) )
    {
        int d[1000] = {0};
        for(int i=0; i<s.size(); i++)
         if( s[i] >='a' && s[i]<='z' )
         {
            it = xh.find(s[i]);
            d[s[i]-'a'] ++;
            if(it->second == 0)
                xh.insert(pair<char, int> (s[i], 1 ) );
            else
                 if(d[s[i]-'a'] == 1 ) (it->second)++ ;
            }

    }
    file.close();


    for (it = xh.begin(); it!= xh.end(); ++it)
    {
        std::cout << it->first << ": " << it->second << '\n';
    }
    
    xh.clear();
    
    return 0;
}

đề bài là: Hãy đọc tệp và in ra từng chữ cái cùng số lượng từ chứa chữ cái này. Kết quả in sắp theo thứ tự từ điển. Không in những chữ cái không xuất hiện trong tệp.
e không hiểu dòng " if(d[s[i]-‘a’] == 1 ) (it->second)++ ;" , có ai giải thích giúp e với ạ

Posts: 12

Participants: 3

Read full topic


Viewing all articles
Browse latest Browse all 34346

Trending Articles