#include #include <unordered_map> #include

struct Table1 { int key1; int key2; std::string value; };

struct Table2 { int key1; int key2; double value; };

int main() { std::vector table1 = {{1, 2, "value1"}, {3, 4, "value2"}, {5, 6, "value3"}}; std::vector table2 = {{1, 2, 1.2}, {3, 4, 3.4}, {7, 8, 5.6}};

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;

}

两表进行左连接,两表各选择前两列作为键值,使用结构体及哈希表,重新生成对应C++代码

原文地址: https://www.cveoy.top/t/topic/vRB 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录