本文共 4046 字,大约阅读时间需要 13 分钟。
#include #include #include #include #include #include #include void ReadDataFromFile(std::string &filename, std::vector > &lines_feat) { std::ifstream vm_info(filename.c_str()); std::string lines, var; std::vector row; lines_feat.clear(); while(!vm_info.eof()) { getline(vm_info, lines); if(lines.empty()) break; std::stringstream stringin(lines); row.clear(); while(stringin >> var) { row.push_back(var); } lines_feat.push_back(row); }}template void Display2DVector(std::vector > &vv) { /*field0 field1 field2...*/ for(size_t i=0;i ::const_iterator it=vv.at(i).begin(); it!=vv.at(i).end(); ++it) { std::cout<<*it<<" "; } std::cout<<"\n"; } std::cout<<"--------the total rows of the raw data is: "< <<" rows.\n";}template void DisplayMapData(std::map > &mymap) { /*the format of map_data is (key, vector), in which value is a vector of optional info*/ for(typename::std::map >::const_iterator it=mymap.begin(); it!=mymap.end(); ++it) { std::cout< first<<", "; typename::std::vector ::const_iterator sit; for(sit=it->second.begin(); sit!=it->second.end(); ++sit) { std::cout<<*sit<<" "; } std::cout<<"\n"; } std::cout<<"--------the total records of the Map is: "< <<"\n";}template void Vectors2Multimap(std::vector > &vv, std::multimap > &mymap) { /*-----convert the 2d vector(the original data) to multimap(ip, )-------*/ for(size_t i=0; i vm_s; vm_s.clear(); for(typename::std::vector ::const_iterator it=vv.at(i).begin();it!=vv.at(i).end();++it) { size_t len=vv.at(i).size(); if(len == 1) { vm_s.push_back("value is none"); } else if(len > 1 && field) { vm_s.push_back(*it); field++; } else { field++; } } mymap.insert(make_pair(vv.at(i).front(), vm_s)); }}template void ProcessFun(std::vector > &vv, std::map > &mymap) { typedef typename::std::multimap >::const_iterator I; std::multimap > mulmap; Vectors2Multimap(vv, mulmap); std::ofstream outfile; outfile.open("out.csv", std::ios::out); for(typename::std::map >::const_iterator it=mymap.begin(); it!=mymap.end(); ++it) { std::string ip=it->first; std::cout< <<"*********************************************************\n"; std::pair Info=mulmap.equal_range(ip); typename::std::vector ::const_iterator sit; if(Info.second==Info.first) { for(sit=it->second.begin(); sit!=it->second.end(); ++sit) { std::cout< <<", "<<*sit<<", 未反馈"< v_ac; v_ac.clear(); for(I i=Info.first; i!=Info.second; ++i) { v_ac.push_back(i->second.front()); } for(sit=it->second.begin(); sit!=it->second.end(); ++sit) { typename::std::vector ::const_iterator find_it; find_it=std::find(v_ac.begin(), v_ac.end(), *sit); if(find_it == v_ac.end()) { std::cout< <<", "<<*sit<<", 未反馈"< first, ac=i->second.front(); if(a_ip == ip && ac == *sit && i->second.size() > 1) { std::cout<<", "< second.at(1)<<", "; outfile<<", "< second.at(1)<<", "; std::cout< second.back()<<"\n"; outfile< second.back()<<"\n"; } if(a_ip == ip && ac == *sit && i->second.size() == 1) { std::cout< void Vectors2Map(std::vector > &vv, std::map > &mymap) { /*-----convert the 2d vector(the original data) to map(key, value) (ip, )-------*/ for(size_t i=0; i vm_s; vm_s.clear(); for(typename::std::vector ::const_iterator it=vv.at(i).begin();it!=vv.at(i).end();++it) { size_t len=vv.at(i).size(); if(len == 1) { vm_s.push_back("value is none"); } else if(len > 1 && field) { vm_s.push_back(*it); field++; } else { field++; } } mymap.insert(make_pair(vv.at(i).front(), vm_s)); }}int main() { std::map > my_mapa, my_mapb;; std::vector > lines_feata, lines_featb;; std::string filename_a="serverAC.txt", filename_b="boss4a.txt"; /*read data from file to 2d vector*/ ReadDataFromFile(filename_a, lines_feata); ReadDataFromFile(filename_b, lines_featb); /*display the raw data*/ //Display2DVector(lines_feata); //Display2DVector(lines_featb); /*convert the 2d vectors to map*/ Vectors2Map(lines_feata, my_mapa); //DisplayMapData(my_mapa); ProcessFun(lines_featb, my_mapa); return 0;}
转载于:https://www.cnblogs.com/donggongdechen/p/10524315.html