49 lines
1.3 KiB
C++
49 lines
1.3 KiB
C++
#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; // 程序结束
|
||
}
|