两表进行左连接,两表各选择前两列作为键值,使用结构体及哈希表,重新生成对应C++代码
#include
struct Table1 { int key1; int key2; std::string value; };
struct Table2 { int key1; int key2; double value; };
int main() {
std::vector
std::unordered_map<std::pair<int, int>, std::string> map1;
std::unordered_map<std::pair<int, int>, double> map2;
for (const auto& row : table1) {
map1[std::make_pair(row.key1, row.key2)] = row.value;
}
for (const auto& row : table2) {
map2[std::make_pair(row.key1, row.key2)] = row.value;
}
std::cout << "key1\tkey2\tvalue1\tvalue2\n";
for (const auto& entry : map1) {
int key1 = entry.first.first;
int key2 = entry.first.second;
std::string value1 = entry.second;
double value2 = map2[entry.first];
std::cout << key1 << "\t" << key2 << "\t" << value1 << "\t" << value2 << "\n";
}
return 0;
}
原文地址: https://www.cveoy.top/t/topic/vRB 著作权归作者所有。请勿转载和采集!