打印(字符串)两个数组的所有元素的所有组合

时间:2019-04-16 10:02:44

标签: c++

如何打印两个数组元素的所有可能组合,它们都使用字符串? 输入:{“ Tom”,“ John”,“ Alex”}        {“狮子座”,“玛丽”} 输出:汤姆-利奥,约翰-利奥         亚历克斯-玛丽,约翰-玛丽         亚历克斯-利奥,汤姆-玛丽

1 个答案:

答案 0 :(得分:0)

容易!只需遍历两个数组并打印两个结果即可:

// you might want to pick a different size other than 10
std::array<std::string, 10> array1;
std::array<std::string, 10> array2;

// ... fill contents of arrays

// Iterate over both arrays and print each string combination
for (const auto& string1 : array1) {
    for (const auto& string2 : array2) {
        std::cout << string1 << ' ' << string2 << '\n';
    }
}

数组也可以这样声明:

std::array<std::string, 3> array1 {"Tom", "John", "Alex"};
相关问题