下面是一个MergeSort
实现。我的问题是编译器抱怨std::begin
不能应用于可变大小的数组 temp
以便进一步使用std:copy
.
我正在使用 C++17 和gcc 8.3。
template<typename Container, typename Iterator>
void Search::MergeSort(Container &array, Iterator begin, Iterator end)
{
auto const len = end - begin;
if (len > 1)
{
auto mid = begin + len / 2;
MergeSort(array, begin, mid);
MergeSort(array, mid, end);
typename Container::value_type temp[len];
int p = 0;
for (auto i = begin, j = mid; i < mid; ++i)
{
auto curr = *i;
while (j < end && *j < curr) temp[p++] = *j++;
temp[p++] = curr;
}
auto temp_begin = std::begin(temp); // ! problem: unable to compile this line
copy(temp_begin, temp_begin + p, begin);
}
错误消息包括:
template argument deduction/substitution failed:
note: mismatched types 'std::initializer_list<_Tp>' and 'std::vector<int>::value_type*' {aka 'int*'}
variable-sized array type 'std::vector<int>::value_type [len]' {aka 'int [len]'} is not a valid template argument