我在哪里放置一个重载的运算符函数来输出数组

时间:2011-11-02 04:11:38

标签: c++

有人能告诉我这是否正确。我正试图重载我的<<我的动态数组类中的数组的运算符。

Specefically,

我可以在课程中加入std:ostream吗?这似乎并不严谨。编译器错误告诉我它应该只接受一个参数。

void print_array(std::ostream &os = cout) 
  { 
  for (int i = 0; i < size; i++) os << array[i] << endl; 
  } 
    std::ostream& operator<<(std::ostream& stream, dynamic_array const& data) 
  { 
  data.print_array(stream);
  return stream; 
  }

动态数组类

/*
Needs a reszie function added
Merge sort is better for sequential, stable(equal elements not re-arranged, or
*/
#include "c_arclib.cpp"

template <class T> class dynamic_array
  {
  private:
    T* array;
    T* scratch;
    void merge_recurse(int left, int right)
      {
      if(right == left + 1)
        {
        return;
        }
      else
        {
        int i = 0;
        int length = right - left;
        int midpoint_distance = length/2;
        int l = left, r = left + midpoint_distance;
        merge_recurse(left, left + midpoint_distance);
        merge_recurse(left + midpoint_distance, right);
        for(i = 0; i < length; i++)
          {
          if((l < (left + midpoint_distance)) && (r == right || array[l] > array[r]))
            {
            scratch[i] = array[l];
            l++;
            }
          else
            {
            scratch[i] = array[r];
            r++;
            }
          }
        for(i = left; i < right; i++)
          {
          array[i] = scratch[i - left];
          }
        }
      }
    void quick_recurse(int left, int right) 
      {  
      int l = left, r = right, tmp;
      int pivot = array[(left + right) / 2];
      while (l <= r)
        {
        while (array[l] < pivot)l++;
        while (array[r] > pivot)r--;
        if (l <= r) 
          {
          tmp = array[l];
          array[l] = array[r];
          array[r] = tmp;
          l++;
          r--;
          }
        }
      if (left < r)quick_recurse(left, r);
      if (l < right)quick_recurse(l, right);
      }  
  public:
    int size;
    dynamic_array(int sizein)
      {
      size=sizein;
      array = new T[size]();
      }
    void print_array(std::ostream &os = cout) 
      { 
      for (int i = 0; i < size; i++) os << array[i] << endl; 
      } 
    std::ostream& operator<<(std::ostream& stream, dynamic_array const& data) 
      { 
      data.print_array(stream);
      return stream; 
      }
    void print_array()
      {
      for (int i = 0; i < size; i++) cout << array[i] << endl;
      }
    int merge_sort()
      {
      scratch = new T[size]();
      if(scratch != NULL)
        {
        merge_recurse(0, size);
        return 1;
        }
      else
        {
        return 0;
        }
      }
    void quick_sort()
      {
      quick_recurse(0,size);
      }
    void rand_to_array()
      {
      srand(time(NULL));
      int* k;
      for (k = array; k != array + size; ++k)                                             
        { 
        *k=rand();                                      
        } 
      }
    void order_to_array()
      {
      int* k;
      int i = 0;
      for (k = array; k != array + size; ++k)                                             
        { 
        *k=i;
        ++i;        
        } 
      }
    void rorder_to_array()
      {
      int* k;
      int i = size;
      for (k = array; k != array + size; ++k)                                             
        { 
        *k=i;
        --i;        
        } 
      }
  };
int main()
  {
  dynamic_array<int> d1(1000000);
  d1.order_to_array();
  clock_t time_start=clock();
  d1.merge_sort(); 
  clock_t time_end=clock();
  double result = (double)(time_end - time_start) / CLOCKS_PER_SEC; 
  cout << result;
  }

2 个答案:

答案 0 :(得分:1)

将操作员放在班级之外:

template <class T> class dynamic_array
{
    ...
    // if you need to use protected/private members:
    friend std::ostream& operator<<(std::ostream& stream,
                                    dynamic_array<T> const& data);
}
template <class T>
std::ostream& operator<<(std::ostream& stream, dynamic_array<T> const& data) 
{ 
    data.print_array(stream);
    return stream; 
}

答案 1 :(得分:1)

由于operator<<的左操作数将是ostream而不是动态数组对象,因此需要将operator<<实现为全局函数,而不是成员函数(成员函数重载始终是调用为object.operator<<(argument),因此其成员的对象必须为左操作数。

如果需要访问对象的内部,可以将其设为好友。如果(在这种情况下)它只使用对象的公共接口(在你的情况下是print_array),那么它可以只是一个普通的全局函数。

template <class T>
class dynamic_array {
    // ...
};

template <class T>
std::ostream &operator<<(std::ostream &os, dynamic_array<T> const &a) { 
    a.print_array(os);
    return os;
}

但就个人而言,我可能会让print_array成为私人成员,并让全球operator<<成为朋友。这减少了类的公共接口(即只有一种方法来打印dynamic_array而不是两个)而不会丢失功能(因为do 正好相同的东西)。

相关问题