博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
multimap的使用 in C++,同一个关键码存在多个值
阅读量:5239 次
发布时间:2019-06-14

本文共 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

你可能感兴趣的文章
jquery的contains方法
查看>>
linux后台运行和关闭SSH运行,查看后台任务
查看>>
CAN总线波形中ACK位电平为什么会偏高?
查看>>
MyBatis课程2
查看>>
桥接模式-Bridge(Java实现)
查看>>
Spring的JdbcTemplate、NamedParameterJdbcTemplate、SimpleJdbcTemplate
查看>>
Mac下使用crontab来实现定时任务
查看>>
303. Range Sum Query - Immutable
查看>>
图片加载失败显示默认图片占位符
查看>>
【★】浅谈计算机与随机数
查看>>
解决 sublime text3 运行python文件无法input的问题
查看>>
javascript面相对象编程,封装与继承
查看>>
Atlas命名空间Sys.Data下控件介绍——DataColumn,DataRow和DataTable
查看>>
算法之搜索篇
查看>>
新的开始
查看>>
java Facade模式
查看>>
NYOJ 120校园网络(有向图的强连通分量)(Kosaraju算法)
查看>>
Leetcode 226: Invert Binary Tree
查看>>
http站点转https站点教程
查看>>
解决miner.start() 返回null
查看>>