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

标签 c++ arrays function data-structures

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

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();
} 

关于c++ - 从函数中获取两个数组并将它们存储在 C++ 中的不同数据类型数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74840938/

相关文章:

arrays - 将 VBA 数组元素输出到 Excel 中的一个单元格

sql-server - SQL Server HashBytes 函数

c++ - 在我的主窗口中调用 Qt 小部件主窗口函数

c++ - 如何使文件具有用户输入的名称

javascript - JQuery 问题将对象打印到 console.log

c++ - 转换运算符如何返回值?

php - 使用LIMIT的Mysql表,随机中断

java - 传递给另一个函数时,BufferedReader 可以继续读取文本文件中的行吗?

c++ - 更自然的 boost::bind 替代方案?

c++ - 如果我通过通用引用接受参数,is_rvalue_reference 和 is_lvalue_reference 中的一个是否为真?