添加 '2.1.cpp'
This commit is contained in:
parent
abdedb504c
commit
d3633911a0
93
2.1.cpp
Normal file
93
2.1.cpp
Normal file
@ -0,0 +1,93 @@
|
||||
#include "MySQLWrapper.h"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
using namespace std;
|
||||
|
||||
#include "local_db_account.h"
|
||||
#include "util.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
MySQLConn conn;
|
||||
conn.connect("127.0.0.1",db_user,db_passwd,"bigdata3",3306);
|
||||
cout<<"Connected to DB"<<endl;
|
||||
conn.exec("set names gbk",nullptr);
|
||||
|
||||
map<string,vector<cash_pack>> mp;
|
||||
conn.exec("select job_city,salary from lagou_job",[&](MySQLResult& res)
|
||||
{
|
||||
res.stepRow([&](char** val,unsigned long* len)
|
||||
{
|
||||
string city(val[0]);
|
||||
cash_pack pk=ParseCash(val[1]);
|
||||
mp[Trim(city)].push_back(pk);
|
||||
});
|
||||
});
|
||||
|
||||
conn.exec("use bigdata5",nullptr);
|
||||
conn.exec("select job_city,salary from lagou_job",[&](MySQLResult& res)
|
||||
{
|
||||
res.stepRow([&](char** val,unsigned long* len)
|
||||
{
|
||||
string city(val[0]);
|
||||
cash_pack pk=ParseCash(val[1]);
|
||||
mp[Trim(city)].push_back(pk);
|
||||
});
|
||||
});
|
||||
|
||||
ofstream ofs("result/2.1.csv");
|
||||
|
||||
for(auto iter=mp.begin();iter!=mp.end(); )
|
||||
{
|
||||
bool updated=false;
|
||||
for(auto xiter=mp.begin();xiter!=iter; )
|
||||
{
|
||||
if(iter->first.find(xiter->first)!=string::npos)
|
||||
{
|
||||
/// Found something?
|
||||
/// targetvec.push_back(thisvec) : Add current vector to the previous vector.
|
||||
xiter->second.insert(xiter->second.end(),
|
||||
iter->second.begin(),iter->second.end());
|
||||
|
||||
|
||||
/// iter should now be deleted. Get next iter first
|
||||
auto nowiter=iter;
|
||||
++iter;
|
||||
mp.erase(nowiter);
|
||||
|
||||
/// Stop internal loop.
|
||||
updated=true;
|
||||
break;
|
||||
}
|
||||
else ++xiter;
|
||||
}
|
||||
|
||||
if(!updated) ++iter;
|
||||
}
|
||||
|
||||
#define cout ofs
|
||||
|
||||
for(auto& pr:mp)
|
||||
{
|
||||
cout<<pr.first<<",";
|
||||
int sum=0;
|
||||
int n=pr.second.size();
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
sum+=pr.second[i].ave();
|
||||
}
|
||||
sum/=n;
|
||||
cout<<sum<<endl;
|
||||
}
|
||||
|
||||
#undef cout
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user