【问题标题】:Quick sort with partition not compiling [closed]分区不编译的快速排序[关闭]
【发布时间】:2017-10-26 17:52:39
【问题描述】:

我一直试图编译这段代码,但它似乎不希望分区函数工作。显然这与数组有关,但我无法弄清楚它是什么。

编辑:我使用的是 std::partition,而不是原始分区函数。

#include <ctime>
#include <string>
#include <algorithm>
#include <array>
#include <iostream>
using namespace std;

const int MAX_SIZE = 10000;
const int MIN_SIZE = 10;

这是快速排序:

//quick sort
void quickSort(int arr[], int first, int last, int size)
{
    if(last - first + 1< MIN_SIZE)
    {
        insertionSort(arr,size);
    }
    else
    {
        int pivotIndex = partition(arr, first, last);
        quickSort(arr, first, pivotIndex-1,size);
        quickSort(arr, pivotIndex+1,last,size);
    }
}

这是错误:

error: no matching function for call to 'partition(int*&, int&, int&)'

感谢任何可以解决此问题的人。除分区功能外的所有功能似乎都可以正常工作。

【问题讨论】:

  • 编译器错误与数组无关。这与partition 函数本身有关。它甚至没有被调用,只是编译器无法理解你试图调用的函数。

标签: c++ arrays sorting quicksort partition


【解决方案1】:

我假设您已经完成了上述代码的using namespace std;。在这种情况下,std::partition 是一种设计用于 STL 容器的算法,而不是原始 C 样式数组。考虑检查参考(例如,here)。

不幸的是,您需要找到/编写适用于原始数组的分区算法,或者从int arr[] 迁移到std::vector&lt;int&gt; 之类的东西。

否则,如果partition确实是您代码中的另一个函数,请同时提供。

希望对您有所帮助。

【讨论】:

  • 谢谢,这两个我都试试。
  • @BenPerkins 还看到了答案的最新编辑,因为到目前为止它只是一个猜谜游戏......
  • 我正在使用 std::partition,我应该包含更多。
  • 没有什么可以阻止您使用带分区的原始数组。 ideone.com/VBDne1en.cppreference.com/w/cpp/algorithm/partition 窃取示例并将其修改为使用原始数组。
猜你喜欢
  • 1970-01-01
  • 2015-10-28
  • 2018-09-13
  • 2021-06-11
  • 2017-08-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多