单词数(set sstream中stringstream的用法)

本文通过解决一个统计不同单词数量的问题,介绍了stringstream如何用于分隔字符串及数据类型转换的方法。

sstream中stringstream的用法

2017年05月17日 19:03:03

阅读数:263

我们先来看一道题,从这道题中就能学到其中一种用法 HDU2072题

 

Problem Description

lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。

 

 

Input

有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。

 

 

Output

每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。

 

 

Sample Input


 

you are my friend #

 

 

Sample Output


 

4

 

 

题意思就是找有多少不同的单词数 不同这个条件很简单,把单词分出来后放到set里直接就会把一样的删除掉,

现在的问题是怎么把这些单词分成一块一块的

这里面stringstream就上场了 我们把输入的字符串传入stringstream实例化的一个对象s中

然后s就相当于电脑里的缓存区我们可以对这个缓存区进行操作

单词之前是以空格分开的,而我们恰好能用到>>这个流是以空格和回车共同作为结束符的;

我们就可以把s缓存区里的数据一点一点的传入set中直到s缓存区里没数据

下面贴一下代码;

 

#include<iostream>
#include <sstream>
#include <set>
#include <string>
using namespace std;
 
int main()
{
 
	char st[10000];
	while(1)
	{
		gets(st);
		if(st[0] == '#')
			break;
 
		set<string>s;
		stringstream ss(st);//将st的数据传入ss缓存区中
		string str;
		while(ss>>str)//ss不为空的时候
		{
			s.insert(str);
		}
		cout<<s.size()<<endl;
	}
	return 0;
}

我们从上面可以看出stringstream实例化的对象其实相当于一个缓存区;那么既然是缓存区我们就可以发现他的另一个功能 也就是类型之间的转换例如string类型编程int或者double型

代码就是

string s = "123456";
int t;
stringstream ss;
ss<<s;
ss>>t;

这样我们就把s的string类型变成了int型

#include <iostream> #include <vector> #include <string> #include <unordered_map> #include <set> #include <algorithm> #include <sstream> #include <cctype> using namespace std; // 函:清理话题字符串,仅保留字母和字,并转换为小写 string cleanTopic(const string& topic) { string cleaned; for (char ch : topic) { if (isalnum(ch)) { cleaned += tolower(ch); } else if (ch == ' ') { cleaned += ' '; } } return cleaned; } // 函:将话题标准化,首字母大写,其余小写,单词间用空格分隔 string normalizeTopic(const string& topic) { stringstream ss(topic); string word, normalized; bool firstWord = true; while (ss >> word) { if (!firstWord) normalized += " "; firstWord = false; if (!word.empty()) { normalized += toupper(word[0]); if (word.size() > 1) { normalized += word.substr(1); } } } transform(normalized.begin(), normalized.end(), normalized.begin(), ::tolower); if (!normalized.empty()) normalized[0] = toupper(normalized[0]); return normalized; } int main() { int N; cin >> N; // 输入微博量 cin.ignore(); // 忽略换行符 unordered_map<string, set<int>> topicMap; // 存储话题及其出现的微博编号 vector<string> tweets(N); // 存储所有微博 for (int i = 0; i < N; ++i) { getline(cin, tweets[i]); // 读取每条微博 string tweet = tweets[i]; size_t pos = 0; while ((pos = tweet.find("#")) != string::npos) { size_t endPos = tweet.find("#", pos + 1); if (endPos == string::npos) break; string rawTopic = tweet.substr(pos + 1, endPos - pos - 1); if (rawTopic.length() > 40) rawTopic = rawTopic.substr(0, 40); // 截取前40个字符 string cleanedTopic = cleanTopic(rawTopic); if (!cleanedTopic.empty()) { topicMap[cleanedTopic].insert(i); // 记录话题及其出现的微博编号 } tweet = tweet.substr(endPos + 1); } } // 找出出现次最多的微博话题 string mostPopularTopic; int maxCount = 0; vector<string> ties; // 存储出现次相同的多个话题 for (const auto& pair : topicMap) { int count = pair.second.size(); if (count > maxCount) { maxCount = count; mostPopularTopic = pair.first; ties.clear(); ties.push_back(mostPopularTopic); } else if (count == maxCount) { ties.push_back(pair.first); } } // 如果有多个话题出现次相同,选择按字母序最小的话题 sort(ties.begin(), ties.end()); string selectedTopic = ties[0]; cout << normalizeTopic(selectedTopic) << endl; // 输出标准化后的话题 cout << maxCount << endl; // 输出该话题出现的微博条 if (ties.size() > 1) { cout << "And " << (ties.size() - 1) << " more ..." << endl; // 输出其他热门话题的量 } return 0; }优化代码
06-17
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值