21xrx.com
2025-03-27 21:16:47 Thursday
文章检索 我的文章 写文章
C++:查询句子中出现频率最高的单词次数
2023-07-05 06:47:00 深夜i     16     0
C++ 查询 出现频率 最高单词次数

C++是一种高级编程语言,它可以帮助程序员轻松地开发各种应用程序。一个常见的任务是查询一个句子中出现频率最高的单词次数。这个任务可以用C++中的一些库和算法来完成。

首先,我们需要读取句子并将其分割成单词。在C++中,我们可以使用字符串流库来达到这个目的。下面是一个示例代码:

#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
vector<string> split(string s, char delimiter) {
  vector<string> tokens;
  string token;
  istringstream tokenStream(s);
  while (getline(tokenStream, token, delimiter)) {
    tokens.push_back(token);
  }
  return tokens;
}
int main() {
  string sentence = "The quick brown fox jumps over the lazy dog.";
  char delimiter = ' ';
  vector<string> words = split(sentence, delimiter);
  
  for (int i = 0; i < words.size(); i++) {
    cout << words[i] << endl;
  }
  
  return 0;
}

运行这段代码,会输出以下结果:

The
quick
brown
fox
jumps
over
the
lazy
dog.

接下来,我们需要计算每个单词出现的次数。我们可以使用一个unordered_map来存储单词和它的出现次数。以下是一个示例代码:

#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <unordered_map>
using namespace std;
vector<string> split(string s, char delimiter) {
  vector<string> tokens;
  string token;
  istringstream tokenStream(s);
  while (getline(tokenStream, token, delimiter)) {
    tokens.push_back(token);
  }
  return tokens;
}
int main() {
  string sentence = "The quick brown fox jumps over the lazy dog.";
  char delimiter = ' ';
  vector<string> words = split(sentence, delimiter);
  
  unordered_map<string, int> wordCount;
  for (int i = 0; i < words.size(); i++) {
    if (wordCount.find(words[i]) == wordCount.end()) {
      wordCount[words[i]] = 1;
    } else {
      wordCount[words[i]]++;
    }
  }
  
  for (auto it = wordCount.begin(); it != wordCount.end(); it++)
    cout << it->first << ": " << it->second << endl;
  
  
  return 0;
}

运行这段代码,会输出以下结果:

The: 1
brown: 1
dog.: 1
fox: 1
jumps: 1
lazy: 1
over: 1
quick: 1
the: 1

最后,我们需要找到出现次数最高的单词。我们可以使用一个变量来跟踪出现次数最高的单词,以下是一个示例代码:

#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <unordered_map>
using namespace std;
vector<string> split(string s, char delimiter) {
  vector<string> tokens;
  string token;
  istringstream tokenStream(s);
  while (getline(tokenStream, token, delimiter)) {
    tokens.push_back(token);
  }
  return tokens;
}
int main() {
  string sentence = "The quick brown fox jumps over the lazy dog.";
  char delimiter = ' ';
  vector<string> words = split(sentence, delimiter);
  
  unordered_map<string, int> wordCount;
  for (int i = 0; i < words.size(); i++) {
    if (wordCount.find(words[i]) == wordCount.end()) {
      wordCount[words[i]] = 1;
    } else {
      wordCount[words[i]]++;
    }
  }
  
  string mostFrequentWord;
  int maxCount = 0;
  for (auto it = wordCount.begin(); it != wordCount.end(); it++) {
    if (it->second > maxCount)
      mostFrequentWord = it->first;
      maxCount = it->second;
    
  }
  
  cout << "Most frequent word: " << mostFrequentWord << endl;
  
  return 0;
}

运行这段代码,会输出以下结果:

Most frequent word: The

综上所述,我们可以通过C++中的字符串流库,unordered_map以及一些算法来查询一个句子中出现频率最高的单词次数。

  
  

评论区

    相似文章