添加 '2.3.cpp'
This commit is contained in:
parent
a522a9f76f
commit
df4dc0f3a0
92
2.3.cpp
Normal file
92
2.3.cpp
Normal file
@ -0,0 +1,92 @@
|
||||
#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<int>> mp;
|
||||
conn.exec("select job_city,count(*) from lagou_job group by job_city",[&](MySQLResult& res)
|
||||
{
|
||||
res.stepRow([&](char** val,unsigned long* len)
|
||||
{
|
||||
string city(val[0]);
|
||||
int cc=ParseInt(val[1]);
|
||||
mp[Trim(city)].push_back(cc);
|
||||
});
|
||||
});
|
||||
|
||||
conn.exec("use bigdata5",nullptr);
|
||||
conn.exec("select job_city,count(*) from pages group by job_city",[&](MySQLResult& res)
|
||||
{
|
||||
res.stepRow([&](char** val,unsigned long* len)
|
||||
{
|
||||
string city(val[0]);
|
||||
int cc=ParseInt(val[1]);
|
||||
mp[Trim(city)].push_back(cc);
|
||||
});
|
||||
});
|
||||
|
||||
ofstream ofs("result/2.3.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];
|
||||
}
|
||||
cout<<sum<<endl;
|
||||
}
|
||||
|
||||
#undef cout
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user