Ниже приведена реализация 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