C++ HASH_MAP

写这个小程序主要是想要测试如何使用hash_map进行数据统计,工作中的实际问题是需要将pcap文件中的所有数据包按照数据流四元组<源IP,目的IP,源Port,目的Port>进行统计
// Author: HSW
// Date: 2016/5/15
// 
#include <iostream> 
#include <string> 
#include <vector> 
#include <hash_map> 
using namespace std; 

int main()
{
	hash_map<int, vector<string*>*> IntHashMap; 
	vector<string*>* TmpVector; 
	vector<string> MapKey1; 
	vector<string> MapKey2; 
	MapKey1.push_back("str1"); 
	MapKey1.push_back("str2"); 
	MapKey1.push_back("str1");
	MapKey1.push_back("str2");
	MapKey1.push_back("str1");
	MapKey1.push_back("str2");

	MapKey2.push_back("str1_1");
	MapKey2.push_back("str2_1");
	MapKey2.push_back("str1_2");
	MapKey2.push_back("str2_2");
	MapKey2.push_back("str1_3");
	MapKey2.push_back("str2_3");
	typedef pair<int, vector<string*>* > Makepair; 
	vector<string>::iterator iter1, iter2; 
	hash_map<int, vector<string*>*>::iterator HashMapiter; 
	vector<string*>::iterator pvectoriter; 
	int HashKey; 
	for (iter1 = MapKey1.begin(), iter2 = MapKey2.begin(); iter1 != MapKey1.end(); ++iter1, ++iter2)
	{
		HashKey = hash_value(*iter1); 
		if ((HashMapiter = IntHashMap.find(HashKey))!= IntHashMap.end())
		{
			(*(*HashMapiter).second).push_back(&(*iter2)); 
		}
		else
		{
			TmpVector = new vector<string*>; 
			(*TmpVector).push_back(&(*iter2));
			IntHashMap.insert(Makepair(HashKey, TmpVector)); 
		}
	}
	for (HashMapiter = IntHashMap.begin(); HashMapiter != IntHashMap.end(); ++HashMapiter)
	{
		TmpVector = (*HashMapiter).second; 
		cout << "对应键值 = " << (*HashMapiter).first << endl; 
		for (pvectoriter = (*TmpVector).begin(); pvectoriter != (*TmpVector).end(); ++pvectoriter)
		{
			cout << *(*pvectoriter) << "\t\t\t"; 
		}
		cout << endl; 
	}
	system("pause"); 
	return 0; 
}
运行截图如下:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值