现在的位置: 首页 > 综合 > 正文

Hdu 2072 单词数

2019年04月09日 ⁄ 综合 ⁄ 共 924字 ⁄ 字号 评论关闭

用来练习库函数的使用,第一段代码练习遍历,第二段练习删除。

#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;
}

抱歉!评论已关闭.