如何将数组 2d 作为参数从函数传递并返回数组 2d 此函数
How to pass array 2d as parameter from function and return array 2d this function?
所讨论的问题涉及一个迭代过程,其中对象"am"随此过程(循环(而变化。因此,我需要创建一个"res"对象,该对象将存储Tfunc函数的结果,以便在另一个过程中的同一过程中使用。但是,我不能那样做。我是C++编程的好初学者。我是R语言的程序员。
我的操作系统是使用 ide 代码块的 ubuntu 16.04
。#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
const int k = 3;
double Tfunc(double Told[k][k], double amm, int K);
int sample(int x);
int main(){
int i, j;
double am = 50;
double told[k][k]{
{1,0,0},
{0,1,0},
{0,0,1}
};
double res;
res = Tfunc(told, am, k);
for(i=0;i<k;i++){
for(j=0;j<k;j++){
cout << res[i][j] << " ";
}
cout << "n";
}
return 0;
}
double Tfunc(double Told[k][k], double amm, int K)
{
int id1;
int id2;
int id3;
id1 = sample(K);
id2 = sample(K);
id3 = sample(K);
while(id2 == id3){
id3 = sample(K);
}
Told[id1][id2] = Told[id1][id2] + amm;
Told[id1][id3] = Told[id1][id3] - amm;
return Told;
}
int sample(int x)
{
srand(time(NULL)); //initialize the random seed
int RandIndex = rand() % (x);
return RandIndex;
}
首先,也许这是问题的根本原因之一,您没有正确地将二维数组传递给您的子函数。
您可以将其作为引用或指针传递。然后,您还可以在子函数中修改作为参数给出的数组。
请在这里阅读
然后,在现代C++中,您将使用 STL 容器作为您的幼崽。对于二维的东西,你需要创建一个容器容器。所以一个 std::vector of std::vector,或者一个 std::array of std::array。
Tfunc 将返回该容器容器并使用 RVO(返回值优化(。因此,复杂性不会损失。
我为您创建了一个示例文件。这只是一种可能的解决方案。有很多。
#include <iostream>
#include <random>
#include <iterator>
#include <algorithm>
#include <array>
constexpr size_t MatrixDimension = 3;
using DoubleArray = std::array<double, MatrixDimension>;
using Matrix = std::array<DoubleArray, MatrixDimension>;
constexpr Matrix StartMatrix{{
{1.0, 0.0, 0.0},
{0.0, 1.0, 0.0},
{0.0, 0.0, 1.0}
}};
size_t randomIndex()
{
std::random_device randomDevice; // Obtain a random number from hardware
std::mt19937 randomGenerator(randomDevice()); // Seed the generator
std::uniform_int_distribution<size_t> distribution(0, MatrixDimension-1); // Range
return distribution(randomGenerator);
}
Matrix Tfunc(const Matrix& givenMatrix, double amm)
{
size_t index1{ randomIndex() }; // Set indices with random values
size_t index2{ randomIndex() };
size_t index3{ randomIndex() };
while (index2 == index3) { // Make sure that index 2 is not equal index 3
index3 = randomIndex();
}
Matrix calculatedMatrix{};
calculatedMatrix[index1][index2] = givenMatrix[index1][index2] + amm;
calculatedMatrix[index1][index3] = givenMatrix[index1][index3] - amm;
return calculatedMatrix;
}
int main()
{
constexpr double amm{ 50.0 };
Matrix result = Tfunc(StartMatrix, amm); // Apply Tfunc to matrix
// Debug Output. Print matrix to std::cout
std::for_each(result.begin(), result.end(), [](DoubleArray &da) {std::copy(da.begin(), da.end(), std::ostream_iterator<double>(std::cout, " ")); std::cout << 'n'; });
return 0;
}
顺便说一句。我不知道你的程序的目的是什么。但我认为您希望在 TFunc 中拥有 3 个不同的索引。这不能保证。2 可以相同。
我希望这有帮助。 。 。
相关文章:
- 如何在C++函数中声明静态 2D 数组?
- 如何在 C++ 中使用它的构造函数初始化 unique_ptrs 的 2D 向量?
- 将 2D 推力::d evice_vector 复矩阵传递给 CUDA 内核函数
- 如何在构造函数中使用初始值设定项设置具有相同值的 2d 数组?
- 使用指针算法修改函数中的 2D 数组
- 交换函数不是在 C++ 中交换 2D 数组的元素
- 无法分配函数指针的 2D 数组
- 无法将 2D 数组传递到 c++ 中的帮助程序函数中
- 打印 2D 动态数组 c++ 函数
- 如何在类中制作 2D 数组元素,然后在其构造函数中指定其维度?
- 创建整数的 2D 数组,该数组将使用两个函数用随机数填充矩阵.我做错了什么?
- 在 c 和 c++ 中将 2D 数组传递给函数
- 如何创建一个接受2D数组的构造函数
- 我怎么能有一个以2D数组为参数的函数,而数组有一个我想更改的参数/维度
- C++尝试告诉函数要使用哪个 2D 数组
- 如何将 2D 数组传递到函数 (C++)中?
- 将 2D 数组指针传递给 C++ 中的函数
- 将直方图的 2D 数组传递给函数 (C++)?
- 我无法在函数中动态分配 2D 数组
- 2D函数最小化算法或C/C 库