forked from boostorg/array
68 lines
4.0 KiB
HTML
68 lines
4.0 KiB
HTML
![]() |
<HTML>
|
||
|
<HEAD>
|
||
|
<TITLE>array1.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>
|
||
|
array1.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>
|
||
|
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>
|
||
|
Array a = { { 42 } };<BR>
|
||
|
<BR>
|
||
|
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// access elements</FONT></I><BR>
|
||
|
for (unsigned i=1; i<a.size(); ++i) {<BR>
|
||
|
a[i] = a[i-1]+1;<BR>
|
||
|
}<BR>
|
||
|
<BR>
|
||
|
<I><FONT face="Arial,Helvetica,sans-serif" color="0000FF" >// use some common STL container operations</FONT></I><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 << "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>
|
||
|
<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 OK"<BR>
|
||
|
<< std::endl;<BR>
|
||
|
}<BR>
|
||
|
}<BR>
|
||
|
<BR>
|
||
|
</SPAN>
|
||
|
</TT>
|
||
|
</BODY>
|
||
|
</HTML>
|