• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include <string>
9 #include <iostream>
10 #include <boost/array.hpp>
11 
12 template <class T>
13 void print_elements (const T& x);
14 
main()15 int main()
16 {
17     // create array of four seasons
18     boost::array<std::string,4> seasons = {
19         { "spring", "summer", "autumn", "winter" }
20     };
21 
22     // copy and change order
23     boost::array<std::string,4> seasons_orig = seasons;
24     for (std::size_t i=seasons.size()-1; i>0; --i) {
25         std::swap(seasons.at(i),seasons.at((i+1)%seasons.size()));
26     }
27 
28     std::cout << "one way:   ";
29     print_elements(seasons);
30 
31     // try swap()
32     std::cout << "other way: ";
33     std::swap(seasons,seasons_orig);
34     print_elements(seasons);
35 
36     // try reverse iterators
37     std::cout << "reverse:   ";
38     for (boost::array<std::string,4>::reverse_iterator pos
39            =seasons.rbegin(); pos<seasons.rend(); ++pos) {
40         std::cout << " " << *pos;
41     }
42 
43     // try constant reverse iterators
44     std::cout << "reverse:   ";
45     for (boost::array<std::string,4>::const_reverse_iterator pos
46            =seasons.crbegin(); pos<seasons.crend(); ++pos) {
47         std::cout << " " << *pos;
48     }
49     std::cout << std::endl;
50 
51     return 0;  // makes Visual-C++ compiler happy
52 }
53 
54 template <class T>
print_elements(const T & x)55 void print_elements (const T& x)
56 {
57     for (unsigned i=0; i<x.size(); ++i) {
58         std::cout << " " << x[i];
59     }
60     std::cout << std::endl;
61 }
62 
63