#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using
namespace
std;
using
tab_t = vector<vector<string> >;
tab_t tab1{
{
"32"
,
"Rahul"
},
{
"25"
,
"Anshul"
},
{
"17"
,
"Lok"
},
{
"25"
,
"Akil"
},
{
"17"
,
"Anshul"
}
};
tab_t tab2{
{
"Rahul"
,
"Tim"
},
{
"Rahul"
,
"Siva"
},
{
"Anshul"
,
"Gary"
},
{
"Anshul"
,
"Azhar"
},
{
"Lok"
,
"Vamsi"
}
};
ostream& operator<<(ostream& o,
const
tab_t& t)
{
for
(
size_t
i = 0; i < t.size(); ++i) {
o << i <<
":"
;
for
(
const
auto
& e : t[i])
o <<
'\t'
<< e;
o << endl;
}
return
o;
}
tab_t Join(
const
tab_t& a,
size_t
columna,
const
tab_t& b,
size_t
columnb)
{
unordered_multimap<string,
size_t
> hashmap;
for
(
size_t
i = 0;
i < a.size(); ++i) {
hashmap.insert({ a[i][columna], i });
}
tab_t result;
for
(
size_t
i = 0; i < b.size(); ++i) {
auto
range = hashmap.equal_range(
b[i][columnb]);
for
(
auto
it = range.first;
it != range.second; ++it) {
tab_t::value_type row;
row.insert(row.end(),
a[it->second].begin(),
a[it->second].end());
row.insert(row.end(),
b[i].begin(),
b[i].end());
result.push_back(move(row));
}
}
return
result;
}
int
main(
int
argc,
char
const
* argv[])
{
int
ret = 0;
cout <<
"Table A: "
<< endl
<< tab1 << endl;
cout <<
"Table B: "
<< endl
<< tab2 << endl;
auto
tab3 = Join(tab1, 1, tab2, 0);
cout <<
"Joined tables: "
<< endl
<< tab3 << endl;
return
ret;
}