fork download
  1. #include <iostream>
  2. #include <array>
  3.  
  4. template <std::size_t... Idx>
  5. using list = std::index_sequence<Idx...> *;
  6.  
  7. template <std::size_t Count>
  8. using make_indices = std::make_index_sequence<Count> *;
  9.  
  10. template <class T>
  11. constexpr void swap(T& a, T& b) noexcept {
  12. T t = a;
  13. a = b;
  14. b = t;
  15. }
  16.  
  17. template <class T, std::size_t N>
  18. constexpr std::size_t partition(std::array<T, N> &array, std::size_t low, std::size_t high) noexcept {
  19. auto pivot = array[high];
  20. std::size_t i = low;
  21. for (std::size_t j = low; j < high; ++j) {
  22. if (array[j] <= pivot) {
  23. swap(array[i], array[j]);
  24. ++i;
  25. }
  26. }
  27. swap(array[i], array[high]);
  28. return i;
  29. }
  30.  
  31. template<class T, std::size_t N>
  32. constexpr void sort(std::array<T, N>& array, std::size_t low = 0, std::size_t high = N-1) noexcept {
  33. if (low < high && high != ~std::size_t(0)) {
  34. std::size_t p = partition(array, low, high);
  35. sort(array, low, p-1);
  36. sort(array, p+1, high);
  37. }
  38. }
  39.  
  40.  
  41. int main() {
  42. std::array<int, 4> a = {3,2,4,1};
  43. sort(a);
  44. for(int i:a){
  45. std::cout << i;
  46. }
  47. std::array<int, a[3]> b;
  48.  
  49. // your code goes here
  50. return 0;
  51. }
Compilation error #stdin compilation error #stdout 0.01s 5276KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:47:21: error: call to non-‘constexpr’ function ‘std::array<_Tp, _Nm>::value_type& std::array<_Tp, _Nm>::operator[](std::array<_Tp, _Nm>::size_type) [with _Tp = int; long unsigned int _Nm = 4; std::array<_Tp, _Nm>::reference = int&; std::array<_Tp, _Nm>::value_type = int; std::array<_Tp, _Nm>::size_type = long unsigned int]’
  std::array<int, a[3]> b;
                     ^
prog.cpp:47:21: error: call to non-‘constexpr’ function ‘std::array<_Tp, _Nm>::value_type& std::array<_Tp, _Nm>::operator[](std::array<_Tp, _Nm>::size_type) [with _Tp = int; long unsigned int _Nm = 4; std::array<_Tp, _Nm>::reference = int&; std::array<_Tp, _Nm>::value_type = int; std::array<_Tp, _Nm>::size_type = long unsigned int]’
prog.cpp:47:22: note: in template argument for type ‘long unsigned int’
  std::array<int, a[3]> b;
                      ^
stdout
Standard output is empty