49 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include<iostream>
#include<string>
#include<map>
int main()
{
std::cout << "请输入单词:" << std::endl;
std::string word;
std::map<std::string, int>word_data;
while (std::cin >> word)
word_data[word] = word_data[word] + 1;
std::map<std::string, int>::iterator mp_next = word_data.begin();
while (mp_next != word_data.end())
{
std::cout << mp_next->first << " 的出现次数:" << mp_next->second << std::endl;
mp_next++;
}
}
#include <iostream> // 引入标准输入输出流库
#include <string> // 引入字符串库
#include <map> // 引入map容器库
int main()
{
// 输出提示信息
std::cout << "请输入单词:" << std::endl;
std::string word; // 用于存储用户输入的单词
std::map<std::string, int> word_data; // 定义一个map容器key为单词value为单词出现次数
// 循环读取用户输入的单词,直到输入结束(如按 Ctrl+D
while (std::cin >> word)
word_data[word] = word_data[word] + 1; // 更新单词出现次数
// 定义一个迭代器用于遍历map中的所有元素
std::map<std::string, int>::iterator mp_next = word_data.begin();
// 遍历map并输出每个单词及其出现次数
while (mp_next != word_data.end())
{
std::cout << mp_next->first << " 的出现次数:" << mp_next->second << std::endl;
mp_next++; // 移动到下一个元素
}
return 0; // 程序结束
}