简体   繁体   English

c++复制数组内容

[英]Copy the contents of the array in c++

The simplified following code is not correct.下面的简化代码是不正确的。 I want to write into the array YY_ that is supplied to operator() , was wondering how can do it?我想写入提供给operator()的数组 YY_ ,想知道怎么做? Seemingly the issue is that it is assuming that copying pointers copies the contents of the array?!似乎问题是它假设复制指针会复制数组的内容?!

double *computeY(double const *const *xx){
  const int i0 = 0;
  const int i1 = 1;
  static double YY[2];
  YY[0] = xx[i0][0] + xx[i1][0];
  YY[1] = xx[i0][1] + xx[i1][1];
  return YY;
} 

struct computeYFunctor{
   computeYFunctor(){}
   bool operator()(double const *const *xx_, double* YY_) const{
     YY_ = computeY(xx_);
     return true;
  }
 };

Yea, pointers can be difficult.是的,指针可能很困难。 I'm taking a swag at this but I don't have time to check my code, and there are going to be folks here 100,000 times more qualified to do this better then I am... but here we are.我正在接受这个问题,但我没有时间检查我的代码,而且这里的人比我更有资格做这件事 100,000 倍......但我们在这里。

You need to pass the pointer to YY as an argument to computeY()您需要将指向 YY 的指针作为参数传递给 computeY()

void computeY(double const *const *xx, double *YY ){
  const int i0 = 0;
  const int i1 = 1;                // array is  (pointer math)
  YY[0] = xx[i0][0] + xx[i1][0];   // YY[0] is *(YY+0)=val
  YY[1] = xx[i0][1] + xx[i1][1];   // YY[1] is *(YY+1)=val
  // So the two lines YY[0] reached out to the addresses YY+0 and YY+1
  // And modified the data.
} 

struct computeYFunctor{ 
   computeYFunctor(){}
   bool operator()(double const *const *xx_, double* YY_) const{
     computeY(xx_, YY_);
     return true;
  }
 };

Good Luck and I hope this helps.祝你好运,我希望这会有所帮助。 I don't do much operator overloading in my work, so there is a good chance I'm missing something a 12 year old would catch here.我在工作中没有做太多的运算符重载,所以我很有可能错过了 12 岁孩子会在这里看到的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM