您的位置:首页 > 其它

Hdu 2072 单词数

2012-12-20 19:53 441 查看
用来练习库函数的使用,第一段代码练习遍历,第二段练习删除。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;

map<string, int> Map;
map<string, int>::iterator it;

char str[1010];

void init()
{
	Map.clear();
}

int main()
{
	while(gets(str) && strcmp(str, "#"))
	{
		init();
		char *p = strtok(str, " ");
		while(p != NULL)
		{
			Map[p]++;
			p = strtok(NULL, " ");
		}
		int count = 0;
		for(it = Map.begin(); it != Map.end(); it++) if(it->second)
		{
			count++;
		}
		printf("%d\n", count);
	}
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;

map<string, int> Map;
map<string, int>::iterator it;

char str[1010];

void init()
{
	Map.clear();
}

int main()
{
	while(gets(str) && strcmp(str, "#"))
	{
		init();
		char *p = strtok(str, " ");
		while(p != NULL)
		{
			Map[p]++;
			p = strtok(NULL, " ");
		}
		int count = 0;
		for(it = Map.begin(); it != Map.end(); it++) if(it->first == "a")
		{
			Map.erase(it);
		}
		for(it = Map.begin(); it != Map.end(); it++) cout<<it->first<<endl;
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: