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 | |
13 | #include <iostream> |
14 | #include <boost/array.hpp> |
15 | |
16 | int main() |
17 | { |
18 | // define special type name |
19 | typedef boost::array<float,6> Array; |
20 | |
21 | // create and initialize an array |
22 | Array a = { .elems: { 42 } }; |
23 | |
24 | // access elements |
25 | for (unsigned i=1; i<a.size(); ++i) { |
26 | a[i] = a[i-1]+1; |
27 | } |
28 | |
29 | // use some common STL container operations |
30 | std::cout << "size: " << a.size() << std::endl; |
31 | std::cout << "empty: " << (a.empty() ? "true" : "false" ) << std::endl; |
32 | std::cout << "max_size: " << a.max_size() << std::endl; |
33 | std::cout << "front: " << a.front() << std::endl; |
34 | std::cout << "back: " << a.back() << std::endl; |
35 | std::cout << "elems: " ; |
36 | |
37 | // iterate through all elements |
38 | for (Array::const_iterator pos=a.begin(); pos<a.end(); ++pos) { |
39 | std::cout << *pos << ' '; |
40 | } |
41 | std::cout << std::endl; |
42 | |
43 | // check copy constructor and assignment operator |
44 | Array b(a); |
45 | Array c; |
46 | c = a; |
47 | if (a==b && a==c) { |
48 | std::cout << "copy construction and copy assignment are OK" |
49 | << std::endl; |
50 | } |
51 | else { |
52 | std::cout << "copy construction and copy assignment FAILED" |
53 | << std::endl; |
54 | } |
55 | |
56 | return 0; // makes Visual-C++ compiler happy |
57 | } |
58 | |
59 | |