开发者

Using pointers for conditional while/for loops gives error when compiling

开发者 https://www.devze.com 2023-04-11 16:18 出处:网络
I\'m wondering how to properly use pointers in for and while loops in C++. Usually I write using C instead of C++. The only reason I\'m using the C++ std library this time is so I can use the complex

I'm wondering how to properly use pointers in for and while loops in C++. Usually I write using C instead of C++. The only reason I'm using the C++ std library this time is so I can use the complex number functions required by other mathematical functions in the code.

As part of the assignment we were given the following function declaration. The part that I wrote is commented within the function.

 typedef std::complex<double> complex;

 // Evaluates a polynomial using Horner's approach.
 // Inputs:
 //  [coeffs, coeffs_end) - polynomial coefficients, ordered by descending power
 //  x - point of evaluation
 // Outputs:
 //  p - value of polynomial at x
 //  dp - value of polynomial derivative at x
 //  ddp - value of polynomials second derivative at x
 //
 template<typename T>
 inline void poly_val(T const* coeffs, T const* coeffs_end, T x, T & p, T & dp, T & ddp)
  {
      //MY CODE HERE
      int i = 0;
      const T *pnt = coeffs;
      while(pnt != coeffs_end){
                  //Evaluate coefficients for descending powers
          p += coeffs(i)*pow(x,((coeffs_end-1)-i));
          pnt++;
          i++;
      }
 }

The function doesn't know the length of the array, so I'm guessing the stop condition is the pointer 'coeffs_end', which points to the last value in the array 'coeffs'. Can I use a pointer in a conditional this way? (traditionally I would have fed the length of the array into the 开发者_开发问答function, but we cant modify the declarations)

If I do it this way I keep get an error when compiling (which I don't get):

C2064:term foes not evaluate to a function taking 1 arguments

for the following line:

      p += coeffs(i)*pow(x,((coeffs_end-1)-i));


coeffs(i) is calling convention to a function that takes an integer argument. But in your case it is an pointer. So, you need to use [] operator to access the element at it's index.

Also ((coeffs_end-1)-i) resolves to an address location. You need to dereference it to get the value at the location.


Maybe it'd be more readable to write this in a cleaner fashion:

#include <cmath>
#include <iterator>

template<typename T>
inline void poly_val(T const* coeffs, T const* coeffs_end, T x, T & p, T & dp, T & ddp)
{
  const std::size_t nterms = std::distance(coeffs, coeffs_end);
  for (std::size_t i = 0; i != nterms; ++i)
  {
    p += coeffs[i] * std::pow(x, nterms - 1 - i);
  }
}

Since raw pointers can be treated as iterators, we can use std::distance to determine the size of an array bounded by a range [first, last).


Edit: Acutally it can be done even easier:

  for (const T * it = coeffs; it != coeffs_end; ++it)
  {
    p += *it * std::pow(x, std::distance(it, coeffs_end) - 1);
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消