multimap的使用 in C++,同一个关键码存在多个值
#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=;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<<"\n";
}
std::cout<<"--------the total rows of the raw data is: "<<vv.size()<<" rows.\n";
}
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<<"\n";
}
std::cout<<"--------the total records of the Map is: "<<mymap.size()<<"\n";
}
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=; i<vv.size(); ++i) {
size_t field=;
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 == ) {
vm_s.push_back("value is none");
}
else if(len > && 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<<"*********************************************************\n";
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() > ) {
std::cout<<", "<<i->second.at()<<", ";
outfile<<", "<<i->second.at()<<", ";
std::cout<<i->second.back()<<"\n";
outfile<<i->second.back()<<"\n";
}
if(a_ip == ip && ac == *sit && i->second.size() == ) {
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=; i<vv.size(); ++i) {
size_t field=;
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 == ) {
vm_s.push_back("value is none");
}
else if(len > && 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 ;
}
multimap的使用 in C++,同一个关键码存在多个值的更多相关文章
- [jQuery] jQuery如何获取同一个类标签的所有的值
碰巧在开发的时候遇到这个问题,因为jQuery总是只返回第一个类标签的值,所以无法达到我们的要求. 比如: var btn = jQuery('.btn').val(); 获取的只是第一个类标签为bt ...
- Bjarne Stroustrup对C++程序员的忠告
转自:http://blog.csdn.net/adm_qxx/archive/2007/05/20/1617488.aspx 第1章 致读者 [1] 在编写程序时,你是在为你针对某个问题的解决方 ...
- 【STL学习】map&set
技术不只是我的工作,也是我的生活,以后的博客中会穿插一些个人的喜悦.愤怒或者感悟,希望大家能够接受. 我所有的一切,比我技术更好的怕是我的脸皮了,昨天收到京东面试没有通过的消息,喊了几句“我好悲伤啊” ...
- STL编程:C++的忠告!
Copy别人的,有少量修改,可以做为一下参考! C++之父Bjarne Stroustrup 写的 The C++ Programming Language (Special Edition) 中各章 ...
- 深入了解STL中set与hash_set,hash表基础
一,set和hash_set简介 在STL中,set是以红黑树(RB-Tree)作为底层数据结构的,hash_set是以哈希表(Hash table)作为底层数据结构的.set可以在时间复杂度为O(l ...
- 词典(一) 跳转表(Skip table)
词典,顾名思义,就是通过关键码来查询的结构.二叉搜索树也可以作为词典,不过各种BST,如AVL树.B-树.红黑树.伸展树,结构和操作比较复杂,而且理论上插入和删除都需要O(logn)的复杂度. 在词典 ...
- java 超详细面经整理(持续更新)2019.12.19
目录 Java SE 请你解释HashMap中为什么重写equals还要重写hashcode? 请你介绍一下map的分类和常见的情况 请你讲讲Java里面的final关键字是怎么用的? 请你谈谈关于S ...
- 算法-基数排序(radix sort)
本文由@呆代待殆原创,转载请注明出处. 简介:这个排序是原来用在卡片排序机上的一个算法,一般用来比较具有多对关键字域的记录,如日期(年月日),通过基数排序我们会依次对年月日这三个关键字进行排序,只要对 ...
- 2021.12.06 平衡树——Treap
2021.12.06 平衡树--Treap https://www.luogu.com.cn/blog/HOJQVFNA/qian-xi-treap-ping-heng-shu 1.二叉搜索树 1.1 ...
随机推荐
- JS——冒泡排序
核心思想: 1.外层for循环控制比较的轮数 2.内层for循环控制每轮比较的次数 3.外层每进行一轮比较,内层就少一次比较,因为外层每进行一轮比较都会产生一个最大值 <script> v ...
- Python基础——列表、元组操作
列表.元组操作 列表: 列表是Python中最基本的数据结构,列表是最常用的Python数据类型,列表的数据项不需要具有相同的类型.列表中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0 ...
- ASLR(Address space layout randomization)地址空间布局随机化
/********************************************************************* * Author : Samson * Date ...
- 吐得了,vue的多选组合框回显必须是字符串集合
下面这个typeIdList,如果给他赋值,就能回显到页面,但是必须是字符串的集合,如果是数值类型的id,不好意思,请转成字符串
- 移动的 touch事件中的touches、targetTouches和changedTouches
touches: 当前屏幕上所有触摸点的列表; targetTouches: 当前对象上所有触摸点的列表; changedTouches: 涉及当前(引发)事件的触摸点的列表 通过一个例子来区分一下触 ...
- jQuery练习:表单模态框
代码:基于事件冒泡原理和事件委托 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta cha ...
- Mysql - ORDER BY详解
0 索引 1 概述 2 索引扫描排序和文件排序简介 3 索引扫描排序执行过程分析 4 文件排序 5 补充说明 6 参考资料 1 概述 MySQL有两种方式可以实现ORDER BY: 1.通过索引扫描生 ...
- python爬虫18 | 就算你被封了也能继续爬,使用IP代理池伪装你的IP地址,让IP飘一会
我们上次说了伪装头部 ↓ python爬虫17 | 听说你又被封 ip 了,你要学会伪装好自己,这次说说伪装你的头部 让自己的 python 爬虫假装是浏览器 小帅b主要是想让你知道 在爬取网站的时候 ...
- Django-搭建win7虚拟环境-virtualenv
为什么要配置Django虚拟环境? 例如:在开发Python Django的时候,系统安装的Python3只有一个版本:3.6.所有第三方的包都会被pip安装到Python3的site-package ...
- WEB 移动端 CSS3动画性能 优化
很多时候,我们在开发移动端的时候要使自己的网页兼容不同的机型,很多时候会采用CSS3动画,但是很多时候在安卓机下,动画明显会出现卡顿,很难看,那么这里我介绍几个CSS 属性进行硬件加速那么就会得到明显 ...