关于map和hashmap
来源:互联网 发布:淘宝店铺配色方案 编辑:程序博客网 时间:2024/06/05 06:39
今天做的程序猿那题
在公司里面,程序猿经常有一堆todolist要做,而这些todolist是产品经理分配给他们的。但是当程序员遇到不懂技术的产品狗时,就悲剧了。产品经理经常修改他们的todolist,比如:添加,减少他们的todolist。
请设计一个类CodeMonkey ,表示程序猿,另一个类ProductDog,表示产品经理。
- CodeMonkey类有私有成员 name,todolist。构造函数初始化姓名和todolist,公有函数 int sizeof_todolist(), 来自ProductDog类的友元函数 add_todolist(CodeMonkey&,int),reduce_todolist(CodeMonkey& , int);
- ProductDog类有公有函数 add_todolist(CodeMonkey&,int),reduce_todolist(CodeMonkey& , int)
Input Specification
每个测试文件包含一组测试用例,对于每个测试用例,第一行输入 n (1<= n <= 10^6)表示有n个程序员,接下去n行,每行为: name x 表示名为name的程序员的todolist的长度为x。 接下去一行 m (1<=m<=10^6),表示产品经理分配任务的次数。 接下去m行,每行为 name opt x , opt 为 0 表示名为name的程序猿的todolist增加x,opt为 1 表示减少x。 输入数据保证合法性。
Output Specification
对于每个程序员,请输出 name x,x表示最终的todolist长度。按输入顺序输出。
Sample Input:3Jack 1Luck 2Tom 34Tom 0 100Luck 0 50Jack 0 25Tom 1 50Sample Output:
Jack 26Luck 52Tom 53
#include <iostream>#include <string>#include <map>using namespace std;//#define N 101//class CodeMonkey; //注意要先声明//class ProductDog{//public://void add_todolist(CodeMonkey&,int);//void reduce_todolist(CodeMonkey& , int);//};////class CodeMonkey{//private:////string name;//int todolist;////public://map<string,int> mymap;//CodeMonkey(){name="a",todolist=0;}//string getName(){//return name;//}//int getTodo(){//return todolist;//}// int sizeof_todolist();// friend void ProductDog::add_todolist(CodeMonkey&,int);// friend void ProductDog::reduce_todolist(CodeMonkey& , int);// void setName(string s){// name=s;// }// void setTodolist(int s){// todolist=s;// }//};//void ProductDog::add_todolist(CodeMonkey &monkey,int change)//{//int s=monkey.getTodo();//monkey.setTodolist(s+change);//}//void ProductDog::reduce_todolist(CodeMonkey &monkey,int change)//{//int s=monkey.getTodo();//monkey.setTodolist(s-change);//}int main() {//ProductDog dog;int n,m;////CodeMonkey coder;map<string,int>mymap;string aname[100000];//hash_map<string,int> myhacs_map;string name;int todo;cin>>n;typedef pair<string,int> my_pair;for(int i=0;i<n;i++){cin>>name>>todo;aname[i]=name;mymap.insert(my_pair(name,todo));}cin>>m;string myname;int judge,change;while(m--){cin>>myname;cin>>judge>>change;if(judge==0){mymap.find(myname)->second+=change;}else if(judge==1){mymap.find(myname)->second-=change;}} for(int i=0;i<n;i++){ string ss=mymap.find(aname[i])->first; int dd=mymap.find(aname[i])->second; cout<<ss<<" "<<dd<<endl; }return 0;}
主体先用string数组存 题目要求按插入顺序输出
有人说可以采用<string,int,cmp>通过改变cmp的算法去让map按原来序列输出,这是不可取的,这样会破坏map的find的函数
总的来说,如果想find 就别想不排序 通过平衡二叉树实现的要排序
以上纯属个人看法,欢迎讨论~
原帖: http://blog.csdn.net/u011644423
0 0
- 关于map和hashmap
- 关于Map和HashMap的思考
- 遍历HashMap和Map
- map和hashmap
- Map和HashMap
- Map和HashMap
- Map和HashMap
- Map和HashMap
- Map 和HashMap
- HashMap和Map
- Map HashMap和LinkedHashMap区别
- Map接口和HashMap练习
- Map的hashMap和TreeMap
- HashMap和Map的道道
- 关于HashMap和LinkedHashMap
- 关于hashmap和hashtable
- 关于HashMap和HashTable
- stl: map和hashmap比较 hashmap与hashtable
- sql判断时间范围
- python 字典排序
- linux6.5下 简单配置 MINI 系统实现详解(1)
- 10年后再看Robust Real-Time Face Detection(二) 之特征讨论
- 【leetcode】 Text Justification
- 关于map和hashmap
- Spring框架概述
- API-基本数据类型与字符串的互换
- 线程池ThreadPoolExecutor参数设置
- uva 562 Dividing coins
- 安全书籍等推荐
- AndroidMainifest标签使用说明2——<activity>
- pylucence lucence的Python实现安装过程
- 一颗排序二叉树,令f=(最大值+最小值)/2,找出距离f值最近、大于f值的结点