fork download
  1. #include <initializer_list>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <memory>
  5.  
  6. template <class T> class test
  7. {
  8. private:
  9. std::unique_ptr<T[]> arr;
  10. public:
  11. std::size_t rows;
  12. std::size_t cols;
  13. test(std::initializer_list<std::initializer_list<T>> r) : rows(r.size())
  14. {
  15. using init = const std::initializer_list<T>;
  16. cols = std::max_element(r.begin(), r.end(), [](init& lhs, init& rhs)
  17. {return lhs.size() < rhs.size();}
  18. )->size();
  19. arr = std::unique_ptr<T[]>(new T[rows*cols]);
  20. auto source = r.begin();
  21. auto dest = &arr[0];
  22. do {
  23. std::copy(source->begin(), source->end(), dest);
  24. dest += cols;
  25. } while(++source != r.end());
  26. }
  27. T* operator[](std::size_t index)
  28. {
  29. return &arr[0] + index*cols;
  30. }
  31. };
  32.  
  33. int main()
  34. {
  35. test<int> a = {{1, 2, 3}, {4, 5, 6}};
  36. for(std::size_t i = 0; i < a.rows; ++i) {
  37. for(std::size_t j = 0; j < a.cols; ++j)
  38. std::cout << a[i][j] << ' ';
  39. std::cout << '\n';
  40. }
  41. }
  42.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
1 2 3 
4 5 6