在C 中乘以两个char阵列

Multiplying two char arrays in C++

本文关键字:两个 char 阵列      更新时间:2023-10-16

我正在尝试乘以两个char阵列row_indexcol_index,这是我的row_indexcol_index

char row_index[20] = { '1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','J','K','L' };
char col_index[10] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K' };

因此,输出为 CSTRING输出[200] = {'a1','b1','c1',...,'kl'}

是否有执行此类任务的智能/有效方法?我不是在寻找代码。如果有人能为我提供此问题的算法,我将非常感谢!

非常感谢!:)

row_index做一个外循环和col_index的内环,并在内部循环中串联值。该算法的伪代码:

r := 0 
while r < number_of_elements_in(row_index):
    c := 0
    while c < number_of_elements_in(col_index):
        output[r*number_of_elements_in(col_index) + c] := contatenate(col_index[c], row_index[r])
        c = c + 1
    r = r + 1

可能的实现可能是这样的:

#include <iostream>
#include <vector>
int main() {
    std::string row_index = "123456789ABCDEFGHJKL";
    std::string col_index = "ABCDEFGHJK";
    std::vector<std::string> output;
    output.reserve(row_index.size()*col_index.size());
    for(auto r : row_index) {
        for(auto c : col_index) {
            output.emplace_back(std::string() + c + r);
        }
    }
    for(const auto& o : output) // display result
        std::cout << o << "n";
}