⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ip2zone.cc

📁 完成ip到地址id的转换
💻 CC
字号:
#include<fstream>#include<iostream>#include<map>#include<string.h>using namespace std;struct IPInterval{	unsigned int low;	unsigned int high;	unsigned int provinceid;	bool operator==(const IPInterval& ip) const{		if(ip.low>=low && ip.high<=high)		return true;		else		return false;	}	bool operator<(const IPInterval& ip) const{		if(high<ip.low)		return true;		else		return false;	}	bool operator>(const IPInterval& ip) const{		if(low>ip.high)		return true;		else		return false;	}	IPInterval(int l, int h, int id){		low = l;		high = h;		provinceid = id;	}	void set(int l, int h, int id){        low = l;        high = h;        provinceid = id;    }};const int MAXLEN = 40960;char lbuf[MAXLEN];std::map<IPInterval, unsigned int> ipmap;typedef pair <IPInterval, int> Pair;char * delim = "\t";map<IPInterval, unsigned int>::const_iterator itor;static bool binitial = false;bool initializeIP(char * filename){	if(binitial)	return true;	else	binitial = !binitial;	ifstream infile;	infile.open(filename);	int num=0;	if(infile.is_open()){		while(NULL != infile.getline(lbuf, MAXLEN)){		char * p=strtok(lbuf, delim);		unsigned int tmp[3];		int i=-1;		tmp[++i] = atoll(p);		while(p=strtok(NULL, delim)){			tmp[++i] = atoll(p);			if(i>=3)				break;		}		ipmap.insert(Pair(IPInterval(tmp[0], tmp[1], tmp[2]), tmp[2]));		++num;	}		//cerr<<"load ip interval num="<<num<<endl;		return true;	}	cerr<<"ip initialize failed!\n";	return false;}IPInterval ip(0,0,0);int getProvinceID(unsigned int ipnum){	ip.set(ipnum, ipnum, 0);	itor = ipmap.find(ip);	if(itor != ipmap.end())		return itor->second;	else		return -1;}#ifdef MAINint main(int argc, char** argv){	initializeIP("/tmp/ip");	int linenum=0;	ifstream infile;	infile.open("/home/cherokee/dev/ncp/testip");	streambuf* inbuf;	if(infile.is_open()){		inbuf = cin.rdbuf(infile.rdbuf());	}	while(NULL != cin.getline(lbuf, MAXLEN))	{		unsigned int n = atol(lbuf);		ip.set(n, n, 0);		itor = ipmap.find(ip);						if(itor != ipmap.end() )			cout<<ip.low<<"\t"<<itor->second<<"\tlinenum="<<linenum++<<"\n";		else			++linenum;		//	cerr<<linenum++<<"not find\n";	}}#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -