| [12] | 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 | |
|---|
| 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 (unsigned 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 | std::cout << std::endl; |
|---|
| 43 | |
|---|
| 44 | return 0; // makes Visual-C++ compiler happy |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | template <class T> |
|---|
| 48 | void print_elements (const T& x) |
|---|
| 49 | { |
|---|
| 50 | for (unsigned i=0; i<x.size(); ++i) { |
|---|
| 51 | std::cout << " " << x[i]; |
|---|
| 52 | } |
|---|
| 53 | std::cout << std::endl; |
|---|
| 54 | } |
|---|
| 55 | |
|---|