以下是一个C++实现多个字符之间的两两组合,并保存为一个矩阵的示例代码:

#include <iostream>
#include <vector>
#include <string>

std::vector<std::vector<std::string>> generateCombinations(std::vector<char> characters) {
    std::vector<std::vector<std::string>> combinations;
    int n = characters.size();
    
    for (int i = 0; i < n; i++) {
        std::vector<std::string> row;
        for (int j = 0; j < n; j++) {
            std::string combination = "";
            combination += characters[i];
            combination += characters[j];
            row.push_back(combination);
        }
        combinations.push_back(row);
    }
    
    return combinations;
}

int main() {
    std::vector<char> characters = {'a', 'b', 'c', 'd'};
    std::vector<std::vector<std::string>> combinations = generateCombinations(characters);
    
    // 输出矩阵
    for (int i = 0; i < combinations.size(); i++) {
        for (int j = 0; j < combinations[i].size(); j++) {
            std::cout << combinations[i][j] << " ";
        }
        std::cout << std::endl;
    }
    
    return 0;
}

运行以上代码,将输出以下矩阵:

aa ab ac ad 
ba bb bc bd 
ca cb cc cd 
da db dc dd 

每个字符与其他字符的两两组合被保存在一个矩阵中。

c++实现多个字符之间的两两组合并保存为一个矩阵

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

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