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