uva 10815 Andy's First Dictionary

本文介绍了一个使用C++实现的字符串处理程序,该程序能够从输入的文本中提取单词,并利用标准模板库(STL)中的集合(set)来存储这些单词,最终输出所有不重复的单词。本文重点讲解了如何通过字符缓冲区处理英文单词,以及如何利用C++ STL中的集合数据结构进行高效存储。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

字符串加集合应用,注意读数据的结束判断和用scanf不同,应该是gets(buffer)!=NULL这样。

#include <stdio.h>
#include <set>
#include <algorithm>
#include <string>
#include <string.h>
#include <iostream>
using namespace std;

char buffer[250];

set<string> dict;

void split()
{
	int i, len, count;
	char temp[100];
	string str;

	len = strlen(buffer);
	count = 0;
	for(i=0; i<=len; i++)
	{
		if(buffer[i]>='a' && buffer[i]<='z')
			temp[count++] = buffer[i];
		else if(buffer[i]>='A' && buffer[i]<='Z')
			temp[count++] = buffer[i] + ('a'-'A');
		else
		{
			if(count) //防止空行
			{
				str.assign(temp, count);
				dict.insert(str);
			}
			count = 0;
		}
	}
}

int main(void)
{
	dict.clear();

	while(gets(buffer) != NULL)
	{
		split();
		/*
		//这里是本机测试的时候用的
		if(!strcmp(buffer, "over"))
			break;
		*/
	}

	set<string>::iterator it;
	for(it=dict.begin(); it!=dict.end(); it++)
		cout << *it << endl;

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值