c++ - 从函数中获取两个数组并将它们存储在 C++ 中的不同数据类型数组中

我有一个包含多个数组的函数,我想以这种方式在我的主函数中使用这两个数组

void fun ()    //which data type should I place here int OR char ?
{
    int array[4]={1,2,3,4}
    char array2[3]={'a','b','c'}

    return array,array2;
}
int main(){

    int array1[4];
    char array2[3];


    array1=fun();  is it possible to get these array here ?
    array2=fun();
}

最佳答案

如果你真的想返回数组,你可以把它们放在std::arrays的std::pair中:

#include <array>
#include <utility>

auto fun() {
    std::pair<std::array<int, 4>, std::array<char, 3>> rv{
        {1, 2, 3, 4},
        { 'a', 'b', 'c' }
    };

    return rv;
}

int main() {
    auto[ints, chars] = fun();
} 

https://stackoverflow.com/questions/74840938/

相关文章:

r - 如何将整行作为列名?

c++ - 在 C++ 中有没有办法像在 Java 中那样进行垃圾回收?

python - 什么时候值得在 if-else 语句上使用循环?

typescript - 从返回类型推断出窄字符串文字类型

python - 捕获具有正面前瞻性但不匹配模式的组的正则表达式

javascript - 使用 js 正则表达式验证 gsheet/excel 相对范围字符串

c# - 查找过去最近的日期

regex - Linux 提取特定字符串之间的文本

c# - Blazor (.net 7) 中的三种依赖注入(inject)语法有区别吗?

rust - 类型 `&[u8]` 不能被 `usize` 索引?