I tried to write a generic quicksort using iterators but when I compiled it I got this error:
"In instantiation of ‘void QuickSortRec(std::vector, Iter, Iter) [with T = int; Iter = __gnu_cxx::__normal_iterator >]’: Iterators.cpp:49:53: required from here Iterators.cpp:133:28: error: no match for ‘operator/’ (operand types are ‘__gnu_cxx::__normal_iterator >’ and ‘int’) Iter pivot(_begin + (_end / 2));"
This is my code:
template<typename T, typename Iter>
void QuickSortRec(std::vector<T> _vector, Iter _begin, Iter _end)
{
Iter pivot(_begin + (_end / 2));
Iter left(_begin);
Iter right(_end);
while (left <= right)
{
while (*left < *pivot)
{
++left;
}
while (*right > *pivot)
{
--right;
}
if (*left >= *right)
{
Swap(left, right);
++left;
--right;
}
}
if (_begin < right)
{
QuickSortRec(_vector, _begin, right);
}
if (left < _end)
{
QuickSortRec(_vector, left, _end);
}
}
template<typename Iter>
void Swap(Iter _a, Iter _b)
{
Iter temp(_b);
*_b = *_a;
*_a = *temp;
}