array5.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* simple example for using class array<>
  2. * (C) Copyright Nicolai M. Josuttis 2001.
  3. * Distributed under the Boost Software License, Version 1.0. (See
  4. * accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #include <iostream>
  8. #include <boost/array.hpp>
  9. template <typename T>
  10. void test_static_size (const T& cont)
  11. {
  12. int tmp[T::static_size];
  13. for (unsigned i=0; i<T::static_size; ++i) {
  14. tmp[i] = int(cont[i]);
  15. }
  16. for (unsigned j=0; j<T::static_size; ++j) {
  17. std::cout << tmp[j] << ' ';
  18. }
  19. std::cout << std::endl;
  20. }
  21. int main()
  22. {
  23. // define special type name
  24. typedef boost::array<float,6> Array;
  25. // create and initialize an array
  26. const Array a = { { 42.42f } };
  27. // use some common STL container operations
  28. std::cout << "static_size: " << a.size() << std::endl;
  29. std::cout << "size: " << a.size() << std::endl;
  30. // Can't use std::boolalpha because it isn't portable
  31. std::cout << "empty: " << (a.empty()? "true" : "false") << std::endl;
  32. std::cout << "max_size: " << a.max_size() << std::endl;
  33. std::cout << "front: " << a.front() << std::endl;
  34. std::cout << "back: " << a.back() << std::endl;
  35. std::cout << "[0]: " << a[0] << std::endl;
  36. std::cout << "elems: ";
  37. // iterate through all elements
  38. for (Array::const_iterator pos=a.begin(); pos<a.end(); ++pos) {
  39. std::cout << *pos << ' ';
  40. }
  41. std::cout << std::endl;
  42. test_static_size(a);
  43. // check copy constructor and assignment operator
  44. Array b(a);
  45. Array c;
  46. c = a;
  47. if (a==b && a==c) {
  48. std::cout << "copy construction and copy assignment are OK"
  49. << std::endl;
  50. }
  51. else {
  52. std::cout << "copy construction and copy assignment are BROKEN"
  53. << std::endl;
  54. }
  55. typedef boost::array<double,6> DArray;
  56. typedef boost::array<int,6> IArray;
  57. IArray ia = { { 1, 2, 3, 4, 5, 6 } } ; // extra braces silence GCC warning
  58. DArray da;
  59. da = ia;
  60. da.assign(42);
  61. return 0; // makes Visual-C++ compiler happy
  62. }