1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
<HTML>
<HEAD>
<TITLE>array5.cpp</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<TABLE HEIGHT=40 WIDTH="100%">
<TR> <TD ALIGN=LEFT WIDTH="100%" BGCOLOR="#DDDDDD">
<FONT face="Arial,Helvetica" size=+2><B>
array5.cpp
</B></FONT>
</TD></TR></TABLE><BR>
<BR><BR>
<TT>
<SPAN class="Source">
<Font color="0000FF" >/</FONT><I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >* simple example for using class array<></FONT></I><BR>
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >*/</FONT></I><BR>
#include <iostream><BR>
#include <<A href="./array.hpp.html">boost/array.hpp</A>><BR>
<BR>
template <typename T><BR>
void test_static_size (const T& cont)<BR>
{<BR>
int tmp[T::static_size];<BR>
for (unsigned i=0; i<T::static_size; ++i) {<BR>
tmp[i] = int(cont[i]);<BR>
}<BR>
for (unsigned i=0; i<T::static_size; ++i) {<BR>
std::cout << tmp[i] << ' ';<BR>
}<BR>
std::cout << std::endl;<BR>
}<BR>
<BR>
int main()<BR>
{<BR>
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// define special type name</FONT></I><BR>
typedef boost::array<float,6> Array;<BR>
<BR>
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// create and initialize an array</FONT></I><BR>
const Array a = { { 42.42 } };<BR>
<BR>
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// use some common STL container operations</FONT></I><BR>
std::cout << "static_size: " << a.size() << std::endl;<BR>
std::cout << "size: " << a.size() << std::endl;<BR>
std::cout << "empty: " << std::boolalpha << a.empty() << std::endl;<BR>
std::cout << "max_size: " << a.max_size() << std::endl;<BR>
std::cout << "front: " << a.front() << std::endl;<BR>
std::cout << "back: " << a.back() << std::endl;<BR>
std::cout << "[0]: " << a[0] << std::endl;<BR>
std::cout << "elems: ";<BR>
<BR>
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// iterate through all elements</FONT></I><BR>
for (Array::const_iterator pos=a.begin(); pos<a.end(); ++pos) {<BR>
std::cout << *pos << ' ';<BR>
}<BR>
std::cout << std::endl;<BR>
test_static_size(a);<BR>
<BR>
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// check copy constructor and assignment operator</FONT></I><BR>
Array b(a);<BR>
Array c;<BR>
c = a;<BR>
if (a==b && a==c) {<BR>
std::cout << "copy construction and copy assignment are OK"<BR>
<< std::endl;<BR>
}<BR>
else {<BR>
std::cout << "copy construction and copy assignment are BROKEN"<BR>
<< std::endl;<BR>
}<BR>
<BR>
typedef boost::array<double,6> DArray;<BR>
typedef boost::array<int,6> IArray;<BR>
IArray ia = { 1, 2, 3, 4, 5, 6 };<BR>
DArray da;<BR>
da = ia;<BR>
da.assign(42);<BR>
<BR>
return 0; <I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// makes Visual-C++ compiler happy</FONT></I><BR>
}<BR>
<BR>
</SPAN>
</TT>
</BODY>
</HTML>
|