print_array.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2002 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Boost.MultiArray Library
  6. // Authors: Ronald Garcia
  7. // Jeremy Siek
  8. // Andrew Lumsdaine
  9. // See http://www.boost.org/libs/multi_array for documentation.
  10. #include <iostream>
  11. #include "boost/multi_array.hpp"
  12. #include "boost/array.hpp"
  13. #include "boost/cstdlib.hpp"
  14. template <typename Array>
  15. void print(std::ostream& os, const Array& A)
  16. {
  17. typename Array::const_iterator i;
  18. os << "[";
  19. for (i = A.begin(); i != A.end(); ++i) {
  20. print(os, *i);
  21. if (boost::next(i) != A.end())
  22. os << ',';
  23. }
  24. os << "]";
  25. }
  26. void print(std::ostream& os, const double& x)
  27. {
  28. os << x;
  29. }
  30. int main()
  31. {
  32. typedef boost::multi_array<double, 2> array;
  33. double values[] = {
  34. 0, 1, 2,
  35. 3, 4, 5
  36. };
  37. const int values_size=6;
  38. array A(boost::extents[2][3]);
  39. A.assign(values,values+values_size);
  40. print(std::cout, A);
  41. return boost::exit_success;
  42. }
  43. // The output is:
  44. // [[0,1,2],[3,4,5]]