TGStat
TGStat
Type to search
Advanced channel search
  • flag English
    Site language
    flag Russian flag English flag Uzbek
  • Sign In
  • Catalog
    Channels and groups catalog Regional compilations Thematic compilations Платные каналы Search for channels
    Add a channel/group
  • Ratings
    Rating of channels Rating of groups Posts rating
    Ratings of brands and people
  • Analytics
  • Search by posts
  • Telegram monitoring
  • Promotion
    Advertising through Yandex Business Advertising in channels through TGStat Agency Advertising on TGStat.ru website
C/C++ Ready | Программирование

12 Aug, 15:10

Open in Telegram Share Report

Считаем топ самых частых слов на C++!

Иногда нужно быстро понять, какие слова чаще всего встречаются в тексте: в логах, комментариях, поисковых запросах или простом анализаторе.

Сначала заведём счётчик:
std::unordered_map freq;

Разобьём строку на слова:
std::istringstream in(text);
std::string word;

while (in >> word) {
++freq[word];
}

Теперь переложим пары в vector:
std::vector items(
freq.begin(), freq.end()
);

Отсортируем по частоте:
std::sort(items.begin(), items.end(),
[](const auto& a, const auto& b) {
return a.second > b.second;
});

Выведем первые 3 слова:
for (std::size_t i = 0; i < std::min(3, items.size()); ++i) {
std::cout

2.3k 0 17 1 21
Catalog
Channels and groups catalog Channels compilations Search for channels Add a channel/group
Ratings
Rating of Telegram channels Rating of Telegram groups Posts rating Ratings of brands and people
API
API statistics Search API of posts API Callback
Our channels
@TGStat @TGStat_Chat @telepulse @TGStatAPI
Read
Академия TGStat Telegram Research 2019 Telegram Research 2021 Telegram Research 2023
Contacts
Справочный центр Support Email Jobs
Miscellaneous
Terms and conditions Privacy policy Public offer
Our bots
@TGStat_Bot @SearcheeBot @TGAlertsBot @tg_analytics_bot @TGStatChatBot