#include <iostream> #include <string> #include <vector> #include <algorithm> #include <map> #include <fstream> #include <sstream> void ReadDataFromFile(std::string &filename, std::vector<std::vector<std::string> > &lines_feat) { std::ifstream vm_info(filename.c_str()); std::string lines, var; std::vector<std::string> 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 <class T> void Display2DVector(std::vector<std::vector<T> > &vv) { /*field0 field1 field2...*/ for(size_t i=0;i<vv.size();++i) { for(typename::std::vector<T>::const_iterator it=vv.at(i).begin(); it!=vv.at(i).end(); ++it) { std::cout<<*it<<" "; } std::cout<<" "; } std::cout<<"--------the total rows of the raw data is: "<<vv.size()<<" rows. "; } template <class T> void DisplayMapData(std::map<std::string, std::vector<T> > &mymap) { /*the format of map_data is (key, vector), in which value is a vector of optional info*/ for(typename::std::map<std::string, std::vector<T> >::const_iterator it=mymap.begin(); it!=mymap.end(); ++it) { std::cout<<it->first<<", "; typename::std::vector<T>::const_iterator sit; for(sit=it->second.begin(); sit!=it->second.end(); ++sit) { std::cout<<*sit<<" "; } std::cout<<" "; } std::cout<<"--------the total records of the Map is: "<<mymap.size()<<" "; } template <class T> void Vectors2Multimap(std::vector<std::vector<T> > &vv, std::multimap<std::string, std::vector<T> > &mymap) { /*-----convert the 2d vector(the original data) to multimap(ip, <vector>)-------*/ for(size_t i=0; i<vv.size(); ++i) { size_t field=0; std::vector<std::string> vm_s; vm_s.clear(); for(typename::std::vector<T>::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 <class T> void ProcessFun(std::vector<std::vector<T> > &vv, std::map<std::string, std::vector<T> > &mymap) { typedef typename::std::multimap<std::string, std::vector<T> >::const_iterator I; std::multimap<std::string, std::vector<T> > mulmap; Vectors2Multimap(vv, mulmap); std::ofstream outfile; outfile.open("out.csv", std::ios::out); for(typename::std::map<std::string, std::vector<T> >::const_iterator it=mymap.begin(); it!=mymap.end(); ++it) { std::string ip=it->first; std::cout<<ip<<"********************************************************* "; std::pair<I, I> Info=mulmap.equal_range(ip); typename::std::vector<T>::const_iterator sit; if(Info.second==Info.first) { for(sit=it->second.begin(); sit!=it->second.end(); ++sit) { std::cout<<ip<<", "<<*sit<<", 未反馈"<<std::endl; outfile<<ip<<", "<<*sit<<", 未反馈"<<std::endl; } } else { std::vector<std::string> 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<T>::const_iterator find_it; find_it=std::find(v_ac.begin(), v_ac.end(), *sit); if(find_it == v_ac.end()) { std::cout<<ip<<", "<<*sit<<", 未反馈"<<std::endl; outfile<<ip<<", "<<*sit<<", 未反馈"<<std::endl; } else { std::cout<<ip<<", "<<*sit<<", 反馈"<<", 已授权"; outfile<<ip<<", "<<*sit<<", 反馈"<<", 已授权"; for(I i=Info.first; i!=Info.second; ++i) { std::string a_ip=i->first, ac=i->second.front(); if(a_ip == ip && ac == *sit && i->second.size() > 1) { std::cout<<", "<<i->second.at(1)<<", "; outfile<<", "<<i->second.at(1)<<", "; std::cout<<i->second.back()<<" "; outfile<<i->second.back()<<" "; } if(a_ip == ip && ac == *sit && i->second.size() == 1) { std::cout<<std::endl; outfile<<std::endl; } } } } } } outfile.close(); } template <class T> void Vectors2Map(std::vector<std::vector<T> > &vv, std::map<std::string, std::vector<T> > &mymap) { /*-----convert the 2d vector(the original data) to map(key, value) (ip, <vector>)-------*/ for(size_t i=0; i<vv.size(); ++i) { size_t field=0; std::vector<std::string> vm_s; vm_s.clear(); for(typename::std::vector<T>::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<std::string, std::vector<std::string> > my_mapa, my_mapb;; std::vector<std::vector<std::string> > 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; }