set ,map 容器

前一阵自学深度学习,很有没有回来写博客了,编程不能扔,所以继续写,最近参加编程的网站答题,发现自己太菜了,根本不会,脑袋转不过来,真的觉得自己太low了,所以还需要从底层来。还是老套路从题目中看代码。


给一个数据表


输入

The first line contains an integer N denoting the number of lists. (1 ≤ N ≤ 1000)

Then follow N blocks. Each block describes one list.  

The first line of each block contains an integer M denoting the number of products in the list. (1 ≤ M ≤ 50)

M lines follow. Each line contains the product id, the purchase date and the price.  

输出

The products that appear in all of the lists. You should output the product id in increasing order.  

If two different product share the same id (with different price) you should output the id twice.  


样例输入
3  
2  
1111-1111 07/23/2016 998.00  
1111-2222 07/23/2016 888.00  
2  
1111-2222 07/23/2016 888.00  
1111-1111 07/23/2016 998.00  
2 
1111-2222 07/23/2016 888.00  
1111-1111 07/23/2016 999.00 
样例输出
1111-2222

#include <iostream>
#include <map>
#include <set>
#include <string>


using namespace std;


int main()
{
map<string, int> buyer_data;
int buyer_data_num;
cin >> buyer_data_num;
// cout << buyer_data_num;
for (int i = 0; i < buyer_data_num; i++)
{
set<string> data;
int num;
cin >> num;
// cout << num;
for (int j = 0; j < num; j++)
{
string id, date, price;
cin >> id >> date >> price;
string temp;
temp.append(id);
temp.append(date);
temp.append(price);
// cout << temp;
data.insert(temp);
}
for (auto& a_t : data)
{
// cout << a_t;
buyer_data[a_t] ++;
}
}


for (auto & b_t : buyer_data)
{
if (b_t.second == buyer_data_num)
{
cout << b_t.first.substr(0, 9)<<endl;
}
}
}


一开始想只用vector储存,对于大多数案例其实没毛病,但是如果有一个list里面对一个数据存放两次那么数据真的判断不出来到底是不是来自同一个list的,所以如果真的想用vector的话,也需要用set先去排列一下数据。


在写程序之前,真的不知道map和set有什么用,感觉自己好菜,其实map有点像字典,其实他就是地点,主键不能相等,值能够相等。而且map竟然不用初始化,直接顶一个定义一个mao ,map<string,int> a ; a["1"] = 1 ; 这样就可以插入一个【“1” : 1】的字典,而且 map <string,int> a ; a["2"] ++ ;  这样得到的“2” 对应的数值竟然是1 ,没催应该是默认对整数型的数字初始为0 . 至于set ,我我感觉就是一个排序的容器,当然存在两个相等的数据时直接忽略一个,比如存在多个1,insert至set容器中只剩下一个1 . 这个在本次的程序中,为防止同一个list中里面添加同一条数据多次。 本次程序中的map其实可以换成vector,但是相当麻烦,因为之后还要对vector数据统计。 感觉用map真的方便了很多。 这里面for 多次用到 , c++ auto真的很方便,这样直接定义for(auto & k : arr) 直接索引出arr  , 对于map 访问他的时候与访问pair很相似 , 第一个变量就。first  ,第二个变量就是 .second . 接下来的每星期,我会不定时的更新博客的,主要是从编程角度来学c++,希望大家支持。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值