Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 带顺序开关的插入排序功能_C++_Algorithm_Sorting_Insertion Sort - Fatal编程技术网

C++ 带顺序开关的插入排序功能

C++ 带顺序开关的插入排序功能,c++,algorithm,sorting,insertion-sort,C++,Algorithm,Sorting,Insertion Sort,我试图编写一个插入排序函数,该函数将根据参数顺序的符号在升序和降序之间切换。它可以工作,但看起来不太正确,因为我用作开关的条件运算符会给内部循环的每次迭代增加一些开销。因此,我想征求您的意见,如何编写更好的函数版本 void Array::sort(int order) //Array is a struct that contains a vector of pointers named vect, as well as this function. { if (order==0) ret

我试图编写一个插入排序函数,该函数将根据参数顺序的符号在升序和降序之间切换。它可以工作,但看起来不太正确,因为我用作开关的条件运算符会给内部循环的每次迭代增加一些开销。因此,我想征求您的意见,如何编写更好的函数版本

void Array::sort(int order) //Array is a struct that contains a vector of pointers named vect, as well as this function.
{
  if (order==0) return;
  bool ascending = (order>0);
  int i,j;
  Data* buffer; //Data is a struct containing some variables, including the key that has to be sorted.
  for (i=1; i<_size; i++)
  {
    buffer = vect[i]; //A vector of pointers to Data objects declared in the Array struct.
    j=i-1;
    while ( j>=0 && (ascending?(vect[j]->key > buffer->key):(vect[j]->key < buffer->key)))
    {
      vect[j+1] = vect[j];
      j--;
    }
    vect[++j] = buffer;
  }
}

一种选择是使用模板,并将函数重新定义为

template<class T> void Array::sort(T op)
{
    ...
    while ( j>=0 && op(vect[j]->key,buffer->key))
    ...
}
然后可以使用适当的排序对象调用排序

struct LessThan : public std::binary_function<int, int, bool>   {
    bool operator() (int x, int y) const { return x < y; }
};
struct GreaterThan : public std::binary_function<int, int, bool>    {
    bool operator() (int x, int y) const { return x > y; }
};

Array::sort(LessThan());

如果你真的想要性能,你可以写两个函数,而不是一个。但这会导致重复。这是C++在模板中闪耀的地方。

本质上,你想写两个函数,每个函数都知道它的排序顺序是静态的,在编译时,选择哪个调用动态。 最简单的变化是:

// original code, templated
template <bool Ascending>
void Array::sort() {
  int i,j;
  Data* buffer;
  for (i=1; i<_size; i++) {
    buffer = vect[i];
    j=i-1;
    while (j>=0 && (Ascending?(vect[j]->key > buffer->key):(vect[j]->key < buffer->key)))
    {
      vect[j+1] = vect[j];
      j--;
    }
    vect[++j] = buffer;
  }
}

// original prototype
void Array::sort(int order) {
  if (order > 0)
    sort<true>();
  else if (order < 0)
    sort<false>;
}

投反对票是为了什么?我猜要么有人认为它没有显示出研究成果,要么认为它的措辞对更广泛的受众没有帮助。不是说我同意,但这些是向上/向下投票悬停文本中的条件。抱歉,我不理解比较/比较部分。我添加了一些解释。为了进一步阅读,您需要查找术语函子或函数对象。
// Comparitor is some type you can call to compare two arguments
template <typename Comparitor>
void Array::sort(Comparitor comp) {
  int i,j;
  Data* buffer;
  for (i=1; i<_size; i++) {
    buffer = vect[i];
    j=i-1;
    while (j>=0 && comp(vect[j]->key, buffer->key)) {
      vect[j+1] = vect[j];
      j--;
    }
    vect[++j] = buffer;
  }
}

// std::greater and less come from <functional>
void Array::sort(int order) {
  typedef decltype(vect[0]->key) KeyType; // or use the real type directly
  if (order > 0)
    sort(std::greater<KeyType>());
  else if (order < 0)
    sort(std::less<KeyType>());
}