array1.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* simple example for using class array<>
  2. *
  3. * (C) Copyright Nicolai M. Josuttis 2001.
  4. * Distributed under the Boost Software License, Version 1.0. (See
  5. * accompanying file LICENSE_1_0.txt or copy at
  6. * http://www.boost.org/LICENSE_1_0.txt)
  7. *
  8. * Changelog:
  9. * 20 Jan 2001 - Removed boolalpha use since stock GCC doesn't support it
  10. * (David Abrahams)
  11. */
  12. #include <iostream>
  13. #include <boost/array.hpp>
  14. int main()
  15. {
  16. // define special type name
  17. typedef boost::array<float,6> Array;
  18. // create and initialize an array
  19. Array a = { { 42 } };
  20. // access elements
  21. for (unsigned i=1; i<a.size(); ++i) {
  22. a[i] = a[i-1]+1;
  23. }
  24. // use some common STL container operations
  25. std::cout << "size: " << a.size() << std::endl;
  26. std::cout << "empty: " << (a.empty() ? "true" : "false") << std::endl;
  27. std::cout << "max_size: " << a.max_size() << std::endl;
  28. std::cout << "front: " << a.front() << std::endl;
  29. std::cout << "back: " << a.back() << std::endl;
  30. std::cout << "elems: ";
  31. // iterate through all elements
  32. for (Array::const_iterator pos=a.begin(); pos<a.end(); ++pos) {
  33. std::cout << *pos << ' ';
  34. }
  35. std::cout << std::endl;
  36. // check copy constructor and assignment operator
  37. Array b(a);
  38. Array c;
  39. c = a;
  40. if (a==b && a==c) {
  41. std::cout << "copy construction and copy assignment are OK"
  42. << std::endl;
  43. }
  44. else {
  45. std::cout << "copy construction and copy assignment FAILED"
  46. << std::endl;
  47. }
  48. return 0; // makes Visual-C++ compiler happy
  49. }