fork download
  1. #include <iostream>
  2. #include <array>
  3. #include <initializer_list>
  4. #include <algorithm>
  5.  
  6. using std::cout;
  7. using std::cin;
  8. using std::endl;
  9.  
  10. /*
  11.  * T = Type
  12.  * S = Size
  13.  */
  14. template<typename T, size_t S>
  15. class Array {
  16. public:
  17. using value_type = T;
  18. using size_type = size_t;
  19. using different_type = ptrdiff_t;
  20. using pointer = T*;
  21. using const_pointer = const T*;
  22. using reference = T&;
  23. using const_reference = const T&;
  24.  
  25. constexpr int Size() const { return S; }
  26.  
  27. reference operator[](size_t index) { return m_Data[index]; }
  28. const_reference operator[](size_t index) const { return m_Data[index]; }
  29.  
  30. Array(std::initializer_list<T> args)
  31. {
  32. std::copy_n(args.begin(), std::min(args.size(), S), std::begin(m_Data));
  33. if (args.size() < S)
  34. std::fill_n(std::begin(m_Data)+args.size(), S-args.size(), T{});
  35. }
  36.  
  37. private:
  38. T m_Data[S];
  39. };
  40.  
  41. int main()
  42. {
  43. // My array
  44. Array<int, 10> arr = { 32, 45, 12 };
  45. for (int i = 0; i < arr.Size(); ++i) {
  46. cout << arr[i] << ' ';
  47. }
  48.  
  49. cout << endl;
  50.  
  51. // std::array used as reference
  52. std::array<int, 10> test = { 32, 45, 12, 53 };
  53. for(auto val : test) {
  54. cout << val << ' ';
  55. }
  56.  
  57. cin.get();
  58. return 0;
  59. }
Success #stdin #stdout 0s 4876KB
stdin
Standard input is empty
stdout
32 45 12 0 0 0 0 0 0 0 
32 45 12 53 0 0 0 0 0 0