65,186
社区成员




MyPair MaxValue(hash_map<MyString, int, MyHashCompare<MyString>> ipMap)//求每个hash_map最大值
{
hash_map<MyString,int>::iterator iter = ipMap.begin();
MyString maxStr=(*iter).first;
int maxNum=(*iter).second;
iter++;
int countNum=0;
//int* intArray=new int[10*1024*1024];//10M
while (iter != ipMap.end())
{
if (maxNum<(*iter).second)
{
maxStr=(*iter).first;
maxNum=(*iter).second;
}
iter++;
}
//int maxNum=MaxValue(intArray,countNum);
//delete[] intArray;
MyPair tempPair;
tempPair.first=maxStr;
tempPair.second=maxNum;
return tempPair;
}
[/quote]
你这里的ipMap已经构建好了,我是指
int best = 0, value = 0;
foreach(int ip: 某个源) // 其中所有的ip的开头那个数字一样
{
const int mask = (1<<24)-1;
if (++cnt[ip&mask] > best)
value = ip, best = cnt[ip&mask];
}
[/quote]
你的思路也不太明白。。。关于这道题能否给个编程思路(你的理解)或纠正下我的编程思路?
大数据文件,我不知道怎么构造,我只做了一个简单的模拟。
编码思路:1、生成若干个子文件;2、对于每个小文件,可以构建一个IP作为key,出现次数作为value的hash_map;3、获取每个子文件中访问次数最多的键值对;4、获取所有文件中访问次数最多的键值对,即对每个子文件中访问最多的键值对求取最大值。
#include "StdAfx.h"
#include <atlstr.h>
#include <hash_map>
#include <string>
#include <iostream>
#include <time.h>
using namespace std;
using namespace stdext;
#define FileNum (1024)
//typedef CString ClassA;
class MyString: public CString{
public:
inline size_t hash_value(const MyString& str) const
{
size_t value = _HASH_SEED;
size_t size = str.GetLength();
if (size > 0) {
size_t temp = (size / 16) + 1;
size -= temp;
for (size_t idx = 0; idx <= size; idx += temp) {
value += (size_t)str[(int)idx];
}
}
return(value);
}
};
template<class _Tkey>
class MyHashCompare : public hash_compare<_Tkey>
{
public:
size_t operator()(const _Tkey& _Key) const
{
return(_Key.hash_value(_Key));//此处需要注意更改
}
bool operator()(const _Tkey& _Keyval1, const _Tkey& _Keyval2) const
{
return (comp(_Keyval1, _Keyval2));
}
};
typedef pair<MyString, int> MyPair;
MyPair MaxValue(hash_map<MyString, int, MyHashCompare<MyString>> ipMap)//求每个hash_map最大值
{
hash_map<MyString,int>::iterator iter = ipMap.begin();
MyString maxStr=(*iter).first;
int maxNum=(*iter).second;
iter++;
int countNum=0;
//int* intArray=new int[10*1024*1024];//10M
while (iter != ipMap.end())
{
if (maxNum<(*iter).second)
{
maxStr=(*iter).first;
maxNum=(*iter).second;
}
iter++;
}
//int maxNum=MaxValue(intArray,countNum);
//delete[] intArray;
MyPair tempPair;
tempPair.first=maxStr;
tempPair.second=maxNum;
return tempPair;
}
MyPair MaxKey(MyPair* tempPairs, int num)
{
MyPair tempPair;
MyString maxStr=tempPairs[0].first;
int maxNum=tempPairs[0].second;
for (int i=1; i<num; i++)
{
if (maxNum<tempPairs[i].second)
{
maxStr=tempPairs[i].first;
maxNum=tempPairs[i].second;
}
}
tempPair.first=maxStr;
tempPair.second=maxNum;
return tempPair;
}
int main()
{
hash_map<MyString, int, MyHashCompare<MyString>> ipMap[FileNum];
FILE* fpData[FileNum];
MyPair tempPairs[FileNum];
int iFileNum=0;
//FILE* fp=fopen("data.txt","wb");
//生成5个文件
MyString strTemp1,strTemp2;
for (int i=101; i<=200; i++)
{
//strTemp.Format("192.168.0.%d-%d|",i,rand());
if ((i-1)%20==0)
{
MyString strTemp;
strTemp.Format("data%d.txt",++iFileNum);
fpData[iFileNum-1]=fopen((LPSTR)(LPCTSTR)strTemp,"wb");//fpData[0]==data1.txt
}
strTemp1.Format("192.168.0.%d\r\n",i);
fwrite(strTemp1,1,strTemp1.GetLength(),fpData[iFileNum-1]);
strTemp2.Format("%d\r\n",rand());
if (i%20==0)
{
strTemp2.Format("%d",rand());//最后一行不换行
}
fwrite(strTemp2,1,strTemp2.GetLength(),fpData[iFileNum-1]);
//hmap.insert(MyPair(strTemp,i+1));
}
for (int j=0; j<iFileNum; j++)
{
fclose(fpData[j]);
}
//5个文件的IP和访问次数存入hash_map
for (int k=0; k<iFileNum; k++)
{
MyString strTemp;
strTemp.Format("data%d.txt",k+1);
fpData[k]=fopen((LPSTR)(LPCTSTR)strTemp,"rb");//fpData[0]==data1.txt
while (!feof(fpData[k]))
{
char tempIP[25];
MyString strIP;
MyString strNum;
//fgets((LPSTR)(LPCTSTR)strIP,25,fpData[k]);//读取一行,第二个参数为最大长度
fgets(tempIP,25,fpData[k]);
strIP.Format("%s",tempIP);//此处strIP=tempIP会出错,MyString为CString的子类
strIP.TrimRight();//去除右边处空格(不去除strIP包含空行字符,会被strNum覆盖)
cout<< "strIP==" << strIP<< endl;
fgets((LPSTR)(LPCTSTR)strNum,15,fpData[k]);
cout<< "strNum==" << _ttoi(strNum)<< endl;
ipMap[k].insert(MyPair(strIP,_ttoi(strNum)));
//cout<< "strIP==" << MyPair(strIP,_ttoi(strNum)).first<< endl;
//cout<< "strNum==" << MyPair(strIP,_ttoi(strNum)).second<< endl;
}
}
//int intArray[5]={8,10,6,5,3};
//cout << "Max==" << MaxValue(intArray,5)<< endl;
for (int k=0; k<iFileNum; k++){
tempPairs[k].first=MaxValue(ipMap[k]).first;
tempPairs[k].second=MaxValue(ipMap[k]).second;
cout << "Max==" << tempPairs[k].first << "--" << tempPairs[k].second<< endl;
}
MyPair m_maxPair=MaxKey(tempPairs,iFileNum);
cout << "Max==" << m_maxPair.first << "--" << m_maxPair.second<< endl;//访问次数最多的IP
for (int j=0; j<iFileNum; j++)
{
fclose(fpData[j]);
}
system("pause");
return 0;
}
//文件1中的内容排序并去重,结果保存到文件2中
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXCHARS 128 //能处理的最大行宽,包括行尾的\n和字符串尾的\0
int MAXLINES=10000,MAXLINES2;
char *buf,*buf2;
int c,n,hh,i,L;
FILE *f;
char ln[MAXCHARS];
int ignore_case=0;
int icompare(const void *arg1,const void *arg2) {
return stricmp((char *)arg1,(char *)arg2);
}
int compare(const void *arg1,const void *arg2) {
return strcmp((char *)arg1,(char *)arg2);
}
int main(int argc,char **argv) {
if (argc<3) {
printf("Unique line. Designed by zhao4zhong1@163.com. 2012-08-20\n");
printf("Usage: %s src.txt uniqued.txt [-i]\n",argv[0]);
return 1;
}
if (argc>3) ignore_case=1;//若存在命令行参数3,忽略大小写
f=fopen(argv[1],"r");
if (NULL==f) {
printf("Can not find file %s!\n",argv[1]);
return 1;
}
buf=(char *)malloc(MAXLINES*MAXCHARS);
if (NULL==buf) {
fclose(f);
printf("Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES,MAXCHARS);
return 2;
}
n=0;
hh=0;
i=0;
while (1) {
if (NULL==fgets(ln,MAXCHARS,f)) break;//
hh++;
L=strlen(ln)-1;
if ('\n'!=ln[L]) {//超长行忽略后面内容
printf("%s Line %d too long(>%d),spilth ignored.\n",argv[1],hh,MAXCHARS);
while (1) {
c=fgetc(f);
if ('\n'==c || EOF==c) break;//
}
}
while (1) {//去掉行尾的'\n'和空格
if ('\n'==ln[L] || ' '==ln[L]) {
ln[L]=0;
L--;
if (L<0) break;//
} else break;//
}
if (L>=0) {
strcpy(buf+i,ln);i+=MAXCHARS;
n++;
if (n>=MAXLINES) {
MAXLINES2=MAXLINES*2;
if (MAXLINES2==1280000) MAXLINES2=2500000;
buf2=(char *)realloc(buf,MAXLINES2*MAXCHARS);
if (NULL==buf2) {
printf("Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES2,MAXCHARS);
printf("WARNING: Lines >%d ignored.\n",MAXLINES);
break;//
}
buf=buf2;
MAXLINES=MAXLINES2;
}
}
}
fclose(f);
if (n>1) {
if (ignore_case) qsort(buf,n,MAXCHARS,icompare);
else qsort(buf,n,MAXCHARS,compare);
}
f=fopen(argv[2],"w");
if (NULL==f) {
free(buf);
printf("Can not create file %s!\n",argv[2]);
return 2;
}
fprintf(f,"%s\n",buf);
if (n>1) {
if (ignore_case) {
hh=0;
L=MAXCHARS;
for (i=1;i<n;i++) {
if (stricmp((const char *)buf+hh,(const char *)buf+L)) {
fprintf(f,"%s\n",buf+L);
}
hh=L;
L+=MAXCHARS;
}
} else {
hh=0;
L=MAXCHARS;
for (i=1;i<n;i++) {
if ( strcmp((const char *)buf+hh,(const char *)buf+L)) {
fprintf(f,"%s\n",buf+L);
}
hh=L;
L+=MAXCHARS;
}
}
}
fclose(f);
free(buf);
return 0;
}
MyPair MaxValue(hash_map<MyString, int, MyHashCompare<MyString>> ipMap)//求每个hash_map最大值
{
hash_map<MyString,int>::iterator iter = ipMap.begin();
MyString maxStr=(*iter).first;
int maxNum=(*iter).second;
iter++;
int countNum=0;
//int* intArray=new int[10*1024*1024];//10M
while (iter != ipMap.end())
{
if (maxNum<(*iter).second)
{
maxStr=(*iter).first;
maxNum=(*iter).second;
}
iter++;
}
//int maxNum=MaxValue(intArray,countNum);
//delete[] intArray;
MyPair tempPair;
tempPair.first=maxStr;
tempPair.second=maxNum;
return tempPair;
}
[/quote]
你这里的ipMap已经构建好了,我是指
int best = 0, value = 0;
foreach(int ip: 某个源) // 其中所有的ip的开头那个数字一样
{
const int mask = (1<<24)-1;
if (++cnt[ip&mask] > best)
value = ip, best = cnt[ip&mask];
}
MyPair MaxValue(hash_map<MyString, int, MyHashCompare<MyString>> ipMap)//求每个hash_map最大值
{
hash_map<MyString,int>::iterator iter = ipMap.begin();
MyString maxStr=(*iter).first;
int maxNum=(*iter).second;
iter++;
int countNum=0;
//int* intArray=new int[10*1024*1024];//10M
while (iter != ipMap.end())
{
if (maxNum<(*iter).second)
{
maxStr=(*iter).first;
maxNum=(*iter).second;
}
iter++;
}
//int maxNum=MaxValue(intArray,countNum);
//delete[] intArray;
MyPair tempPair;
tempPair.first=maxStr;
tempPair.second=maxNum;
return tempPair;
}