fork download
  1. // begin() and end() functions for arrays (C++03)
  2. //
  3. // Written in 2012 by Martinho Fernandes
  4. //
  5. // To the extent possible under law, the author(s) have dedicated all copyright and related
  6. // and neighboring rights to this software to the public domain worldwide. This software is
  7. // distributed without any warranty.
  8. //
  9. // You should have received a copy of the CC0 Public Domain Dedication along with this software.
  10. // If not, see <http://c...content-available-to-author-only...s.org/publicdomain/zero/1.0/>.
  11.  
  12. // Installation:
  13. // Place this into a header file and #include it in your code. Done.
  14. //
  15. // Usage:
  16. // int array[10]; // create an array of ten ints
  17. // int* first = so::begin(array); // get a pointer to the beginning
  18. // int* last = so::end(array); // get a pointer to one past the end
  19. // size_t n = so::size(array); // get the size of the array (10)
  20.  
  21. #ifndef SO_BEGIN_END_HPP_INCLUDED
  22. #define SO_BEGIN_END_HPP_INCLUDED
  23.  
  24. #include <cstddef> // size_t
  25.  
  26. namespace so {
  27. template <typename T, std::size_t N>
  28. T* begin(T(&arr)[N]) { return &arr[0]; }
  29.  
  30. template <typename T, std::size_t N>
  31. T* end(T(&arr)[N]) { return &arr[0] + N; }
  32.  
  33. template <typename T, std::size_t N>
  34. std::size_t size(T(&)[N]) { return N; }
  35. }
  36.  
  37. #endif
  38.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty