• 统计指定字符串中每个单词出现的次数(C++)


    统计指定字符串中每个单词出现的次数

    字符串分割单词的方法

    stringstream类简介

    在程序中如果想要使用stringstream,必须要包含头文件。在该头文件下,标准库三个类:istringstreamostringstreamstringstream,分别用来进行流的输入、输出和输入输出操作

    stringstream主要可以用来:

    1. 将数值类型数据格式化为字符串
    int a = 12345678;
     string sa;
     // 将一个整形变量转化为字符串,存储到string类对象中
     stringstream s;
     s << a;
     s >> sa;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    注意: 注意多次转换时,必须使用clear()将上次转换状态清空掉
    stringstreams在转换结尾时(即最后一个转换后),会将其内部状态设置为badbit
    因此下一次转换是必须调用clear()将状态重置为goodbit才可以转换
    但是clear()不会将stringstreams底层字符串清空掉

    s.str(""); // 将stringstream底层管理string对象设置成"", 否则多次转换时,会将结果全部累积在底层string对象中

    1. 字符串拼接
       stringstream sstream;
       // 将多个字符串放入 sstream 中
       sstream << "first" << " " << "string,";
       sstream << " second string";
       cout << "strResult is: " << sstream.str() << endl;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述
    总结:
    1. stringstream实际是在其底层维护了一个string类型的对象用来保存结果。
    2. 多次数据类型转化时,一定要用clear()来清空,才能正确转化,但clear()不会将stringstream底层的string对象清空。
    3. 可以使用s. str("")方法将底层string对象设置为 “” 空字符串。
    4. 可以使用s.str()将让stringstream返回其底层的string对象。

    一、 利用输入空格分隔

    vector<string> words;
    string temp;
    while(cin >> temp){
    	words.push_back(temp);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    二、用stringstream空格分隔

        string str;
    	while (getline(cin, str))
    	{
    		stringstream ss(str);
    		string s;
    		vector<string>words;  // 存放单词
    		while (getline(ss, s, ' '))   // 空格截取
    		{
    			words.push_back(s);
    		}
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    题目:

    以字符串形式提供给你一段英文文章,请编写一个程序。该程序将统计指定字符串中每个单词出现的次数。
    我们同时定义单词的长度和出现次数的乘积作为权重,程序最终需要输出权重最高的单词以及其出现的次数
    。(不限语言,函数自行声明)
    要求:需要给出完整可以通过编译的测试用例和测试代码。

    源代码:

    #include
    #include
    #include
    #include
    #include
    using namespace std;
    
    int main()
    {
    	string str;
    	while (getline(cin, str))
    	{
    		stringstream ss(str);
    		string s;
    		vector<string>words;  // 存放单词
    		while (getline(ss, s, ' '))
    		{
    			words.push_back(s);
    		}
    		// 统计的单词出现频次
    		map<string, int> mp;
    		for (size_t i = 0; i < words.size(); i++)
    		{
    			mp[words[i]] += 1;
    		}
    		/*
    		map::iterator it;
    		cout << "字符串单词总个数 : " << words.size() << endl;
    		cout << "不同单词的个数 : " << mp.size() << endl;
    		cout << "不同单词出现的频率 :" << endl;
    		for (it = mp.begin(); it != mp.end(); it++)
    		{
    			cout << it->first << ":" << it->second << "  |  ";
    		}
    		cout << endl;
    		*/
    		cout << "权重最高的单词以及其出现的次数 :" << endl;
    		size_t res = 0;
    		for (size_t i = 0; i < words.size(); i++)
    		{
    			size_t weight = words[i].size() * mp[words[i]];  //权重
    			if (weight > words[res].size() * mp[words[res]])
    				res = i;       // 记录输出结果的下标
    		}
    		cout << words[res] << ":" << mp[words[res]] << endl;
    	}
    	return 0;
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    运行结果:
    在这里插入图片描述

  • 相关阅读:
    四则运算Java版
    使用MLC-LLM将RWKV 3B模型跑在Android手机上
    Docker换国内源和简单操作
    Java优先队列PriorityQueue中的方法和使用细节总结
    【16-配置中心之Nacos的基本使用&Nacos服务之命令空间、Nacos服务之配置组、Nacos服务之配置拆分】
    超级详细的Maven使用教程
    达梦数据库表空间管理常用SQL
    计算机毕业设计ssm餐饮管理系统uto0o系统+程序+源码+lw+远程部署
    0826学习笔记(vim)
    EntityManagerFactory和EntityManager的一个用法探究
  • 原文地址:https://blog.csdn.net/weixin_45910068/article/details/126350499