2000-08-02 18:15:32 +00:00
|
|
|
<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>
|
2001-08-23 20:33:35 +00:00
|
|
|
<< std::endl;<BR>
|
2000-08-02 18:15:32 +00:00
|
|
|
}<BR>
|
|
|
|
else {<BR>
|
2001-08-23 20:33:35 +00:00
|
|
|
std::cout << "copy construction and copy assignment are BROKEN"<BR>
|
|
|
|
<< std::endl;<BR>
|
2000-08-02 18:15:32 +00:00
|
|
|
}<BR>
|
|
|
|
<BR>
|
2000-08-30 12:44:40 +00:00
|
|
|
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>
|
2001-08-23 20:33:35 +00:00
|
|
|
<BR>
|
|
|
|
return 0; <I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// makes Visual-C++ compiler happy</FONT></I><BR>
|
2000-08-02 18:15:32 +00:00
|
|
|
}<BR>
|
|
|
|
<BR>
|
|
|
|
</SPAN>
|
|
|
|
</TT>
|
|
|
|
</BODY>
|
|
|
|
</HTML>
|