fork download
  1. #include <vector>
  2. #include <algorithm>
  3. #include <iterator>
  4. #include <functional>
  5.  
  6. // iterator check
  7. template <typename It, typename T>
  8. constexpr bool is = std::is_same<typename std::iterator_traits<It>::iterator_category, T>::value;
  9.  
  10.  
  11. // quick sort
  12. template <typename BidIt, typename Pred = typename std::less<typename std::iterator_traits<BidIt>::value_type>>
  13. void qsort(BidIt first, BidIt last, Pred compare = {}) noexcept
  14. {
  15. static_assert(is<BidIt, std::bidirectional_iterator_tag> || is<BidIt, std::random_access_iterator_tag>,
  16. "bidirectional iterator required");
  17.  
  18. auto size = std::distance(first, last);
  19.  
  20. if (size > 1) {
  21. using content_type = typename std::iterator_traits<BidIt>::value_type;
  22.  
  23. content_type pivot = *first;
  24. std::vector<content_type> left(size), right(size);
  25. auto left_end = left.begin();
  26. auto right_end = right.begin();
  27.  
  28. for (BidIt i = std::next(first); i != last; ++i) {
  29. compare(*i, pivot) ? *left_end++ = *i : *right_end++ = *i;
  30. }
  31.  
  32. qsort(left.begin(), left_end, compare);
  33. qsort(right.begin(), right_end, compare);
  34.  
  35. std::copy(left.begin(), left_end, first);
  36. *std::next(first, std::distance(left.begin(), left_end)) = pivot;
  37. std::copy(right.begin(), right_end, std::next(first, std::distance(left.begin(), left_end) + 1));
  38. }
  39. }
  40.  
  41. struct integer
  42. {
  43. integer() = delete; //not default constructible
  44. integer(int y):
  45. x(y)
  46. {}
  47.  
  48. int x;
  49. };
  50.  
  51. bool operator<(const integer& lhs, const integer& rhs)
  52. {
  53. return lhs.x < rhs.x;
  54. }
  55.  
  56. int main() {
  57. std::vector<integer> v{1, 0, -1, 4, 2};
  58. qsort(v.begin(), v.end());
  59. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
In file included from /usr/include/c++/6/vector:63:0,
                 from prog.cpp:1:
/usr/include/c++/6/bits/stl_uninitialized.h: In instantiation of ‘static _ForwardIterator std::__uninitialized_default_n_1<true>::__uninit_default_n(_ForwardIterator, _Size) [with _ForwardIterator = integer*; _Size = long unsigned int]’:
/usr/include/c++/6/bits/stl_uninitialized.h:575:20:   required from ‘_ForwardIterator std::__uninitialized_default_n(_ForwardIterator, _Size) [with _ForwardIterator = integer*; _Size = long unsigned int]’
/usr/include/c++/6/bits/stl_uninitialized.h:637:44:   required from ‘_ForwardIterator std::__uninitialized_default_n_a(_ForwardIterator, _Size, std::allocator<_Tp>&) [with _ForwardIterator = integer*; _Size = long unsigned int; _Tp = integer]’
/usr/include/c++/6/bits/stl_vector.h:1309:36:   required from ‘void std::vector<_Tp, _Alloc>::_M_default_initialize(std::vector<_Tp, _Alloc>::size_type) [with _Tp = integer; _Alloc = std::allocator<integer>; std::vector<_Tp, _Alloc>::size_type = long unsigned int]’
/usr/include/c++/6/bits/stl_vector.h:281:30:   required from ‘std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const allocator_type&) [with _Tp = integer; _Alloc = std::allocator<integer>; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<integer>]’
prog.cpp:24:44:   required from ‘void qsort(BidIt, BidIt, Pred) [with BidIt = __gnu_cxx::__normal_iterator<integer*, std::vector<integer> >; Pred = std::less<integer>]’
prog.cpp:58:26:   required from here
/usr/include/c++/6/bits/stl_uninitialized.h:540:37: error: use of deleted function ‘integer::integer()’
    return std::fill_n(__first, __n, _ValueType());
                                     ^~~~~~~~~~~~
prog.cpp:43:2: note: declared here
  integer() = delete; //not default constructible
  ^~~~~~~
stdout
Standard output is empty