如何从 Armadillo 中的函数 return 多值?

how to return multi values from function in Armadillo?

我是 c++ 和犰狳的新手。我写了一个函数如下:

mat solver(mat charge)
{
    mat x = regspace(0, mesh_num - 1);
    mat xx = reshape(x, 1, mesh_num);
    mat xxx = repmat(xx, mesh_num, 1);
    mat y = regspace(0, mesh_num - 1);
    mat yy = reshape(y, mesh_num, 1);
    mat yyy = repmat(yy, 1, mesh_num);
    mat green_func = -0.5 * log(square(step_xc * xxx) + square(step_yc * yyy));
    green_func(0, 0) = 0;
    mat temp_1 = fliplr(green_func);
    mat temp_2 = temp_1.cols(0, mesh_num - 2);
    mat temp_3 = flipud(green_func);
    mat temp_4 = temp_3.rows(0, mesh_num - 2);
    mat temp_5 = fliplr(temp_4);
    mat temp_6 = temp_5.cols(0, mesh_num - 2);
    mat temp_7 = join_horiz(temp_6, temp_4);
    mat temp_8 = join_horiz(temp_2, green_func);
    mat green_fun_expand = join_vert(temp_7, temp_8);
    return green_fun_expand;
}

我想要 return 多个矩阵,例如green_func_expand 和 temp_8。如何实现?

使用元组 STL

tuple <mat, mat>  solver(mat charge)
{
    mat x = regspace(0, mesh_num - 1);
    mat xx = reshape(x, 1, mesh_num);
    mat xxx = repmat(xx, mesh_num, 1);
    mat y = regspace(0, mesh_num - 1);
    mat yy = reshape(y, mesh_num, 1);
    mat yyy = repmat(yy, 1, mesh_num);
    mat green_func = -0.5 * log(square(step_xc * xxx) + square(step_yc * yyy));
    green_func(0, 0) = 0;
    mat temp_1 = fliplr(green_func);
    mat temp_2 = temp_1.cols(0, mesh_num - 2);
    mat temp_3 = flipud(green_func);
    mat temp_4 = temp_3.rows(0, mesh_num - 2);
    mat temp_5 = fliplr(temp_4);
    mat temp_6 = temp_5.cols(0, mesh_num - 2);
    mat temp_7 = join_horiz(temp_6, temp_4);
    mat temp_8 = join_horiz(temp_2, green_func);
    mat green_fun_expand = join_vert(temp_7, temp_8);
    return make_tuple( green_fun_expand,temp_8 ) ;
}

收到此消息时,请尝试

  mat ret1, ret2;
    tie(ret1, ret2) = solver(mat, change);

那你可以直接使用ret1和ret2

或者,您可以使用 class 并将您想要的任何内容添加到 return 作为 class 变量,在函数中创建 class 的对象,填充变量和 return 对象