【问题标题】:How to have a function act on non-argument variables?如何让函数作用于非参数变量?
【发布时间】:2020-08-23 19:02:18
【问题描述】:

我正在尝试编写一个函数,该函数将接收多个索引并根据这些索引处的数组中的值执行某些操作。如果我不将数组声明为函数的参数(它将始终对相同的两个数组进行操作),我会得到 X/Y 未在此范围内声明,但如果我尝试声明它们,我会得到“错误:声明'X' 作为多维数组必须具有除第一个之外的所有维度的边界”,并且边界直到运行时才知道。

有没有办法在函数中做到这一点,还是我每次都必须在 main 中显式编写代码?

代码:

double foo(int A, int B, int t){//A, B, and t are indices 
  int x = X[A][t]-X[B][t]; //X and Y are 2D arrays
  int y = Y[A][t]-Y[B][t];
  double d;
  //do some math ...
  return d;
}

int main{
.
.
.
int X[nPeople][tMax]={0};
int Y[nPeople][tMax]={0};
.
.
.
for(int t=0; t<tMax; t++){
  for(int n=0; n<nPeople; n++){
    foo(n,a,t);
  }
}
.
.
.
return 0;
}

【问题讨论】:

  • 建议:动态分配XY
  • 使用std::vector&lt;std::vector&lt;int&gt;&gt;
  • 如果你不将数组作为参数传递,它们应该是全局的...
  • 这些是 C 风格的数组。整个 sn-p 实际上是 C 代码,而不是 C++,否则 foo 将是 XY 是私有成员的类的方法,也很好地解析名称范围。但是对于在编译时不知道大小的声明:是的,要么将它们设为 int * 并动态分配,要么将它们设为 vector 实例以提高安全性。
  • Re: 'int X[nPeople][tMax]={0};" -- 如果在编译时数组大小未知,则此代码不是有效的 C++。一些编译器提供了这样的monstrosity 作为扩展。正如其他人所说,使用std::vector。这就是它的设计目的。

标签: c++ arrays function


【解决方案1】:

有多种方式:

1- 使用包含以下内容的内置 std::vector 库:

#include <vector>

向量是动态数组,因此当您初始化它们时,它们没有严格的容量,您永远无法更改。相反,它们会随着您推动或插入的每个元素而变大。据我所见,您正在处理矩阵,因此您需要二维数组,没问题。这是它的代码:

std::vector<std::vector<int> > 

2- 使用全局变量。在函数之外声明的变量被认为是全局的,因此它们对其下面的每个函数都是可见的。尽管由于它可能导致大规模问题的错误,它不被认为是良好的编码实践,但如果您有足够的信心它不会对您的代码安全/清洁构成威胁,请继续使用它!

3- 使用动态分配的数组并将指针作为参数传递,以便您可以处理任何您想要的大小

【讨论】:

    【解决方案2】:

    函数可以通过多种方式处理数据:

    • 使用全局数据:

      std::vector<std::vector<int>> X;
      std::vector<std::vector<int>> Y;
      
      double foo(int A, int B, int t){ //A, B, and t are indices 
        int x = X[A][t]-X[B][t]; // X and Y are global 2D arrays
        int y = Y[A][t]-Y[B][t];
        double d;
        //do some math ...
        return d;
      }
      

      不幸的是,在示例代码中显示了太多次,但(可变)全局存在很多问题(难以测试、难以重用代码、难以推理代码)

    • 将它们作为参数传递:

      double foo(const std::vector<std::vector<int>>& X, // X and Y are 2D arrays
      
                 const std::vector<std::vector<int>>& Y, // As we can see, so unneeded comment
                 int A, int B, int t){ //A, B, and t are indices 
        int x = X[A][t]-X[B][t];
        int y = Y[A][t]-Y[B][t];
        double d;
        //do some math ...
        return d;
      }
      
    • 使用类绑定它们:

      class C
      {
          std::vector<std::vector<int>> X;
          std::vector<std::vector<int>> Y;
       public:
          double foo(int A, int B, int t){ //A, B, and t are indices 
            int x = X[A][t]-X[B][t]; // X and Y are member 2D arrays (it should be trivial (`this->` might help), so comment not needed neither)
            int y = Y[A][t]-Y[B][t];
            double d;
            //do some math ...
            return d;
          }
      };
      

      注意创建一致的类并避免使用god class(其默认值与全局几乎相同)。

    【讨论】:

      猜你喜欢
      • 2018-10-26
      • 2021-01-19
      • 1970-01-01
      • 2018-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-23
      相关资源
      最近更新 更多