array2.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* 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. #ifndef _SCL_SECURE_NO_WARNINGS
  8. // Suppress warnings from the std lib:
  9. # define _SCL_SECURE_NO_WARNINGS
  10. #endif
  11. #include <algorithm>
  12. #include <functional>
  13. #include <boost/array.hpp>
  14. #include "print.hpp"
  15. using namespace std;
  16. int main()
  17. {
  18. // create and initialize array
  19. boost::array<int,10> a = { { 1, 2, 3, 4, 5 } };
  20. print_elements(a);
  21. // modify elements directly
  22. for (unsigned i=0; i<a.size(); ++i) {
  23. ++a[i];
  24. }
  25. print_elements(a);
  26. // change order using an STL algorithm
  27. reverse(a.begin(),a.end());
  28. print_elements(a);
  29. // negate elements using STL framework
  30. transform(a.begin(),a.end(), // source
  31. a.begin(), // destination
  32. negate<int>()); // operation
  33. print_elements(a);
  34. return 0; // makes Visual-C++ compiler happy
  35. }