multimap应用

**从“map测试输入文档.txt”读入 key-value 数值,对其进行统计,算出 total(总数),count(个数),min(最小值),max(最大值),并写入文件

// MapApplication.cpp : 定义控制台应用程序的入口点。
//VS 2005

#include "stdafx.h"
#include <cassert>
#include <iostream>
#include <typeinfo>
#include <map>

using namespace std;


/*
	因为 string 没有提供 <  操作,这里对 less 类模板进行特化
*/
template<>
struct less<string> : public binary_function<string,string,bool>
{
	bool operator()(const string& _Left, const string& _Right) const
	{
		string::size_type i = 0;
		while(i < _Left.length() && i < _Right.length())
		{
			if(_Left.at(i) != _Right.at(i))
			{
				return _Left.at(i) > _Right.at(i) ? true : false;
			}
			i++;
		}
		
		if(i < _Left.length())
			return true;

		if(i < _Right.length())
			return false;
		
		return false;
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	FILE* pFile = NULL;

	fopen_s(&pFile,"map测试输入文档.txt","r");
	assert(pFile != NULL);
	
	char buf[BUFSIZ + 1]={0};
	
	/*
		把文档内容读入 strRead
	*/
	//fread 函数不会给 buf 自动填充'\0'
	size_t readSize = fread(buf,sizeof(char),BUFSIZ,pFile);
	buf[readSize] = '\0';

	string strRead;
	while(readSize)
	{
		strRead += buf;
		readSize = fread(buf,sizeof(char),BUFSIZ,pFile);
		buf[readSize] = '\0';
	}
	fclose(pFile);
	pFile = NULL;

	/*
		开始处理 strRead
	*/
	string::size_type st;
	string strName,strNumber;
	int nNumber;
	char szName[BUFSIZ + 1];
	multimap<string,int> mm;
	while(!strRead.empty())
	{
		/*
			strRead的形式是 "字母+数字+字母+数字..."
			这里需要把 字母和数字分开
			可以考虑 strtod(); strspn(); strpbrk()函数是否可用
		*/

		//找出字母
		st = strRead.find_first_of("0123456789");
		if(st <= BUFSIZ)
		{
			strRead.copy(szName,st);
			szName[st] = '\0';
			strName = szName;
		}
		strRead.erase(0,st);

		//找出数字
		string::size_type i =0;
		for(i = 0; i < BUFSIZ; i++)
		{
			if(i >= strRead.length() || !isdigit(strRead.at(i)))
				break;
		}
		i++;
		
		if(i <= BUFSIZ)
		{
			strRead.copy(szName,i);
			szName[i] = '\0';
			strNumber = szName;
		}
		strRead.erase(0,i);
		nNumber = atoi(strNumber.c_str());
	
		//存入multimap
		mm.insert(make_pair(strName,nNumber));
 	}

	//写文件
	fopen_s(&pFile,"map测试输出文档.txt","w+");
	assert(pFile != NULL);
	size_t sWrite = fwrite("name \t total \t count \t min \t max \n",sizeof(char),strlen("name \t total \t count \t min \t max \n"),pFile);

	typedef multimap<string,int>::iterator multimapItor;
	typedef multimap<string,int>::size_type sizeType;

	multimapItor Itor;
	pair<multimapItor,multimapItor> p;
	Itor = mm.begin();

	int total,min = 0,max = 0;
	sizeType count;
	while(Itor != mm.end())
	{
		total = 0;
		min = 0;
		max = 0;
		p = mm.equal_range(Itor->first);
		count = mm.count(Itor->first);
		multimapItor mI;

		for(mI = p.first; mI != p.second; mI++)
		{
			if(min == 0 || max == 0)
			{
				min = mI->second;
				max = mI->second;
			}
			total += mI->second;

			//找出最大最小值
			if(mI->second > max)
			{
				max = mI->second;
			}
			
			if(mI->second < min)
			{
				min = mI->second;
			}
		}
		
		char szWrite[BUFSIZ] = {0};
		int len = sprintf(szWrite,"% 10s% 5d% 5d% 5d% 5d\n",Itor->first.c_str(),total,count,min,max);
		szWrite[len] = '\0';
		fwrite(szWrite,sizeof(char),len,pFile);

		if(p.second == mm.end())
			break; 

		Itor = mm.find(p.second->first);
	}

	fclose(pFile);
	system("pause");
	

	return 0;
}


//map测试输入文档.txt

apple 100
pear 103
banana 200
Peach 150
apple 23
pear 54
banana 75
Peach 29
Peach 43
apple 6
pear 21
Peach 150
apple 64
pear 976
banana 2
Peach 124
Peach 543
apple 100
pear 103
banana 200
Peach 150
apple 23
pear 54
banana 75
Peach 29
Peach 43
apple 6
pear 21
Peach 150
apple 64
pear 976
banana 2
Peach 124
Peach 543
apple 100
pear 103
banana 200
Peach 150
apple 23
pear 54
banana 75
Peach 29
Peach 43
apple 6
pear 21
Peach 150
apple 64
pear 976
banana 222
Peach 401
Peacha 402


//map测试输出文档.txt

name total count min max 
    pear 3462   12   21  976
   banana 1051    9    2  222
   apple  579   12    6  100
   Peacha  402    1  402  402
   Peach 2851   17   29  543

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值