mirror of
https://github.com/boostorg/array.git
synced 2025-06-26 20:41:36 +02:00
Compare commits
15 Commits
svn-branch
...
svn-branch
Author | SHA1 | Date | |
---|---|---|---|
e24d329c11 | |||
9804292dad | |||
069b5e2ca1 | |||
526953fc5e | |||
b06f12b0b7 | |||
f0bbb8b211 | |||
d7a5408143 | |||
564e4029d0 | |||
0dc11c2f23 | |||
7da1c4b310 | |||
8f10fdf27e | |||
b41f7fdebe | |||
2fde5e334b | |||
4ee6e051fe | |||
bac6446111 |
546
doc/array.xml
Normal file
546
doc/array.xml
Normal file
@ -0,0 +1,546 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN"
|
||||||
|
"http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
|
||||||
|
<library name="Array" dirname="array" id="array" last-revision="$Date$">
|
||||||
|
<libraryinfo>
|
||||||
|
<author>
|
||||||
|
<firstname>Nicolai</firstname>
|
||||||
|
<surname>Josuttis</surname>
|
||||||
|
</author>
|
||||||
|
|
||||||
|
<copyright>
|
||||||
|
<year>2001</year>
|
||||||
|
<year>2002</year>
|
||||||
|
<year>2003</year>
|
||||||
|
<year>2004</year>
|
||||||
|
<holder>Nicolai M. Josuttis</holder>
|
||||||
|
</copyright>
|
||||||
|
|
||||||
|
<legalnotice>
|
||||||
|
<para>Distributed under the Boost Software License, Version 1.0.
|
||||||
|
(See accompanying file <filename>LICENSE_1_0.txt</filename> or copy at
|
||||||
|
<ulink
|
||||||
|
url="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</ulink>)
|
||||||
|
</para>
|
||||||
|
</legalnotice>
|
||||||
|
|
||||||
|
<librarypurpose>STL compliant container wrapper for arrays of constant size</librarypurpose>
|
||||||
|
<librarycategory name="category:containers"/>
|
||||||
|
</libraryinfo>
|
||||||
|
|
||||||
|
<title>Boost.Array</title>
|
||||||
|
|
||||||
|
<section id="array.intro">
|
||||||
|
<title>Introduction</title>
|
||||||
|
|
||||||
|
<using-namespace name="boost"/>
|
||||||
|
<using-class name="array"/>
|
||||||
|
|
||||||
|
<para>The C++ Standard Template Library STL as part of the C++
|
||||||
|
Standard Library provides a framework for processing algorithms on
|
||||||
|
different kind of containers. However, ordinary arrays don't
|
||||||
|
provide the interface of STL containers (although, they provide
|
||||||
|
the iterator interface of STL containers).</para>
|
||||||
|
|
||||||
|
<para>As replacement for ordinary arrays, the STL provides class
|
||||||
|
<code><classname>std::vector</classname></code>. However,
|
||||||
|
<code><classname>std::vector<></classname></code> provides
|
||||||
|
the semantics of dynamic arrays. Thus, it manages data to be able
|
||||||
|
to change the number of elements. This results in some overhead in
|
||||||
|
case only arrays with static size are needed.</para>
|
||||||
|
|
||||||
|
<para>In his book, <emphasis>Generic Programming and the
|
||||||
|
STL</emphasis>, Matthew H. Austern introduces a useful wrapper
|
||||||
|
class for ordinary arrays with static size, called
|
||||||
|
<code>block</code>. It is safer and has no worse performance than
|
||||||
|
ordinary arrays. In <emphasis>The C++ Programming
|
||||||
|
Language</emphasis>, 3rd edition, Bjarne Stroustrup introduces a
|
||||||
|
similar class, called <code>c_array</code>, which I (<ulink
|
||||||
|
url="http://www.josuttis.com">Nicolai Josuttis</ulink>) present
|
||||||
|
slightly modified in my book <emphasis>The C++ Standard Library -
|
||||||
|
A Tutorial and Reference</emphasis>, called
|
||||||
|
<code>carray</code>. This is the essence of these approaches
|
||||||
|
spiced with many feedback from <ulink
|
||||||
|
url="http://www.boost.org">boost</ulink>.</para>
|
||||||
|
|
||||||
|
<para>After considering different names, we decided to name this
|
||||||
|
class simply <code><classname>array</classname></code>.</para>
|
||||||
|
|
||||||
|
<para>Note that this class is suggested to be part of the next
|
||||||
|
Technical Report, which will extend the C++ Standard (see
|
||||||
|
<ulink url="http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1548.htm">http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1548.htm</ulink>).</para>
|
||||||
|
|
||||||
|
<para>Class <code><classname>array</classname></code> fulfills most
|
||||||
|
but not all of the requirements of "reversible containers" (see
|
||||||
|
Section 23.1, [lib.container.requirements] of the C++
|
||||||
|
Standard). The reasons array is not an reversible STL container is
|
||||||
|
because:
|
||||||
|
<itemizedlist spacing="compact">
|
||||||
|
<listitem><simpara>No constructors are provided.</simpara></listitem>
|
||||||
|
<listitem><simpara>Elements may have an undetermined initial value (see <xref linkend="array.rationale"/>).</simpara></listitem>
|
||||||
|
<listitem><simpara><functionname>swap</functionname>() has no constant complexity.</simpara></listitem>
|
||||||
|
<listitem><simpara><methodname>size</methodname>() is always constant, based on the second template argument of the type.</simpara></listitem>
|
||||||
|
<listitem><simpara>The container provides no allocator support.</simpara></listitem>
|
||||||
|
</itemizedlist>
|
||||||
|
</para>
|
||||||
|
|
||||||
|
<para>It doesn't fulfill the requirements of a "sequence" (see Section 23.1.1, [lib.sequence.reqmts] of the C++ Standard), except that:
|
||||||
|
<itemizedlist spacing="compact">
|
||||||
|
<listitem><simpara><methodname>front</methodname>() and <methodname>back</methodname>() are provided.</simpara></listitem>
|
||||||
|
<listitem><simpara><methodname>operator[]</methodname> and <methodname>at</methodname>() are provided.</simpara></listitem>
|
||||||
|
</itemizedlist>
|
||||||
|
</para>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<library-reference>
|
||||||
|
<header name="boost/array.hpp">
|
||||||
|
<namespace name="boost">
|
||||||
|
<class name="array">
|
||||||
|
<template>
|
||||||
|
<template-type-parameter name="T"/>
|
||||||
|
<template-nontype-parameter name="N">
|
||||||
|
<type>std::size_t</type>
|
||||||
|
</template-nontype-parameter>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<purpose><para>STL compliant container wrapper for arrays of constant size</para></purpose>
|
||||||
|
<typedef name="value_type">
|
||||||
|
<type>T</type>
|
||||||
|
</typedef>
|
||||||
|
<typedef name="iterator">
|
||||||
|
<type>T*</type>
|
||||||
|
</typedef>
|
||||||
|
<typedef name="const_iterator">
|
||||||
|
<type>const T*</type>
|
||||||
|
</typedef>
|
||||||
|
<typedef name="reverse_iterator">
|
||||||
|
<type><classname>std::reverse_iterator</classname><iterator></type>
|
||||||
|
</typedef>
|
||||||
|
<typedef name="const_reverse_iterator">
|
||||||
|
<type><classname>std::reverse_iterator</classname><const_iterator></type>
|
||||||
|
</typedef>
|
||||||
|
<typedef name="reference">
|
||||||
|
<type>T&</type>
|
||||||
|
</typedef>
|
||||||
|
<typedef name="const_reference">
|
||||||
|
<type>const T&</type>
|
||||||
|
</typedef>
|
||||||
|
<typedef name="size_type">
|
||||||
|
<type>std::size_t</type>
|
||||||
|
</typedef>
|
||||||
|
<typedef name="difference_type">
|
||||||
|
<type>std::ptrdiff_t</type>
|
||||||
|
</typedef>
|
||||||
|
|
||||||
|
<static-constant name="static_size">
|
||||||
|
<type>size_type</type>
|
||||||
|
<default>N</default>
|
||||||
|
</static-constant>
|
||||||
|
|
||||||
|
<copy-assignment>
|
||||||
|
<template>
|
||||||
|
<template-type-parameter name="U"/>
|
||||||
|
</template>
|
||||||
|
<parameter name="other">
|
||||||
|
<paramtype>const <classname>array</classname><U, N>&</paramtype>
|
||||||
|
</parameter>
|
||||||
|
<effects><simpara><code>std::copy(rhs.<methodname>begin</methodname>(),rhs.<methodname>end</methodname>(), <methodname>begin</methodname>())</code></simpara></effects>
|
||||||
|
</copy-assignment>
|
||||||
|
|
||||||
|
<method-group name="iterator support">
|
||||||
|
<overloaded-method name="begin">
|
||||||
|
<signature>
|
||||||
|
<type>iterator</type>
|
||||||
|
</signature>
|
||||||
|
<signature cv="const">
|
||||||
|
<type>const_iterator</type>
|
||||||
|
</signature>
|
||||||
|
|
||||||
|
<returns><simpara>iterator for the first element</simpara></returns>
|
||||||
|
<throws><simpara>will not throw</simpara></throws>
|
||||||
|
</overloaded-method>
|
||||||
|
|
||||||
|
<overloaded-method name="end">
|
||||||
|
<signature>
|
||||||
|
<type>iterator</type>
|
||||||
|
</signature>
|
||||||
|
<signature cv="const">
|
||||||
|
<type>const_iterator</type>
|
||||||
|
</signature>
|
||||||
|
|
||||||
|
<returns><simpara>iterator for position after the last element</simpara></returns>
|
||||||
|
<throws><simpara>will not throw</simpara></throws>
|
||||||
|
</overloaded-method>
|
||||||
|
</method-group>
|
||||||
|
|
||||||
|
<method-group name="reverse iterator support">
|
||||||
|
<overloaded-method name="rbegin">
|
||||||
|
<signature>
|
||||||
|
<type>reverse_iterator</type>
|
||||||
|
</signature>
|
||||||
|
<signature cv="const">
|
||||||
|
<type>const_reverse_iterator</type>
|
||||||
|
</signature>
|
||||||
|
|
||||||
|
<returns><simpara>reverse iterator for the first element of reverse iteration</simpara></returns>
|
||||||
|
</overloaded-method>
|
||||||
|
|
||||||
|
<overloaded-method name="rend">
|
||||||
|
<signature>
|
||||||
|
<type>reverse_iterator</type>
|
||||||
|
</signature>
|
||||||
|
<signature cv="const">
|
||||||
|
<type>const_reverse_iterator</type>
|
||||||
|
</signature>
|
||||||
|
|
||||||
|
<returns><simpara>reverse iterator for position after the last element in reverse iteration</simpara></returns>
|
||||||
|
</overloaded-method>
|
||||||
|
</method-group>
|
||||||
|
|
||||||
|
<method-group name="capacity">
|
||||||
|
<method name="size">
|
||||||
|
<type>size_type</type>
|
||||||
|
<returns><simpara><code>N</code></simpara></returns>
|
||||||
|
</method>
|
||||||
|
<method name="empty">
|
||||||
|
<type>bool</type>
|
||||||
|
<returns><simpara><code>N==0</code></simpara></returns>
|
||||||
|
<throws><simpara>will not throw</simpara></throws>
|
||||||
|
</method>
|
||||||
|
<method name="max_size">
|
||||||
|
<type>size_type</type>
|
||||||
|
<returns><simpara><code>N</code></simpara></returns>
|
||||||
|
<throws><simpara>will not throw</simpara></throws>
|
||||||
|
</method>
|
||||||
|
</method-group>
|
||||||
|
|
||||||
|
<method-group name="element access">
|
||||||
|
<overloaded-method name="operator[]">
|
||||||
|
<signature>
|
||||||
|
<type>reference</type>
|
||||||
|
<parameter name="i">
|
||||||
|
<paramtype>size_type</paramtype>
|
||||||
|
</parameter>
|
||||||
|
</signature>
|
||||||
|
|
||||||
|
<signature cv="const">
|
||||||
|
<type>const_reference</type>
|
||||||
|
<parameter name="i">
|
||||||
|
<paramtype>size_type</paramtype>
|
||||||
|
</parameter>
|
||||||
|
</signature>
|
||||||
|
|
||||||
|
<requires><simpara><code>i < N</code></simpara></requires>
|
||||||
|
<returns><simpara>element with index <code>i</code></simpara></returns>
|
||||||
|
<throws><simpara>will not throw.</simpara></throws>
|
||||||
|
</overloaded-method>
|
||||||
|
|
||||||
|
<overloaded-method name="at">
|
||||||
|
<signature>
|
||||||
|
<type>reference</type>
|
||||||
|
<parameter name="i">
|
||||||
|
<paramtype>size_type</paramtype>
|
||||||
|
</parameter>
|
||||||
|
</signature>
|
||||||
|
|
||||||
|
<signature cv="const">
|
||||||
|
<type>const_reference</type>
|
||||||
|
<parameter name="i">
|
||||||
|
<paramtype>size_type</paramtype>
|
||||||
|
</parameter>
|
||||||
|
</signature>
|
||||||
|
|
||||||
|
<returns><simpara>element with index <code>i</code></simpara></returns>
|
||||||
|
<throws><simpara><code><classname>std::range_error</classname></code> if <code>i >= N</code></simpara></throws>
|
||||||
|
</overloaded-method>
|
||||||
|
|
||||||
|
<overloaded-method name="front">
|
||||||
|
<signature>
|
||||||
|
<type>reference</type>
|
||||||
|
</signature>
|
||||||
|
<signature cv="const">
|
||||||
|
<type>const_reference</type>
|
||||||
|
</signature>
|
||||||
|
<requires><simpara><code>N > 0</code></simpara></requires>
|
||||||
|
<returns><simpara>the first element</simpara></returns>
|
||||||
|
<throws><simpara>will not throw</simpara></throws>
|
||||||
|
</overloaded-method>
|
||||||
|
|
||||||
|
<overloaded-method name="back">
|
||||||
|
<signature>
|
||||||
|
<type>reference</type>
|
||||||
|
</signature>
|
||||||
|
<signature cv="const">
|
||||||
|
<type>const_reference</type>
|
||||||
|
</signature>
|
||||||
|
<requires><simpara><code>N > 0</code></simpara></requires>
|
||||||
|
<returns><simpara>the last element</simpara></returns>
|
||||||
|
<throws><simpara>will not throw</simpara></throws>
|
||||||
|
</overloaded-method>
|
||||||
|
|
||||||
|
<method name="data" cv="const">
|
||||||
|
<type>const T*</type>
|
||||||
|
<returns><simpara><code>elems</code></simpara></returns>
|
||||||
|
<throws><simpara>will not throw</simpara></throws>
|
||||||
|
</method>
|
||||||
|
|
||||||
|
<method name="c_array">
|
||||||
|
<type>T*</type>
|
||||||
|
<returns><simpara><code>elems</code></simpara></returns>
|
||||||
|
<throws><simpara>will not throw</simpara></throws>
|
||||||
|
</method>
|
||||||
|
</method-group>
|
||||||
|
|
||||||
|
<method-group name="modifiers">
|
||||||
|
<method name="swap">
|
||||||
|
<type>void</type>
|
||||||
|
<parameter name="other">
|
||||||
|
<paramtype><classname>array</classname><T, N>&</paramtype>
|
||||||
|
</parameter>
|
||||||
|
<effects><simpara><code>std::swap_ranges(<methodname>begin</methodname>(), <methodname>end</methodname>(), other.<methodname>begin</methodname>())</code></simpara></effects>
|
||||||
|
<complexity><simpara>linear in <code>N</code></simpara></complexity>
|
||||||
|
</method>
|
||||||
|
<method name="assign">
|
||||||
|
<type>void</type>
|
||||||
|
<parameter name="value">
|
||||||
|
<paramtype>const T&</paramtype>
|
||||||
|
</parameter>
|
||||||
|
<effects><simpara><code>std::fill_n(<methodname>begin</methodname>(), N, value)</code></simpara></effects>
|
||||||
|
</method>
|
||||||
|
</method-group>
|
||||||
|
|
||||||
|
<data-member name="elems[N]"> <!-- HACK -->
|
||||||
|
<type>T</type>
|
||||||
|
</data-member>
|
||||||
|
|
||||||
|
<free-function-group name="specialized algorithms">
|
||||||
|
<function name="swap">
|
||||||
|
<template>
|
||||||
|
<template-type-parameter name="T"/>
|
||||||
|
<template-nontype-parameter name="N">
|
||||||
|
<type>std::size_t</type>
|
||||||
|
</template-nontype-parameter>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<type>void</type>
|
||||||
|
|
||||||
|
<parameter name="x">
|
||||||
|
<paramtype><classname>array</classname><T, N>&</paramtype>
|
||||||
|
</parameter>
|
||||||
|
<parameter name="y">
|
||||||
|
<paramtype><classname>array</classname><T, N>&</paramtype>
|
||||||
|
</parameter>
|
||||||
|
|
||||||
|
<effects><simpara><code>x.<methodname>swap</methodname>(y)</code></simpara></effects>
|
||||||
|
<throws><simpara>will not throw.</simpara></throws>
|
||||||
|
</function>
|
||||||
|
</free-function-group>
|
||||||
|
|
||||||
|
<free-function-group name="comparisons">
|
||||||
|
<function name="operator==">
|
||||||
|
<template>
|
||||||
|
<template-type-parameter name="T"/>
|
||||||
|
<template-nontype-parameter name="N">
|
||||||
|
<type>std::size_t</type>
|
||||||
|
</template-nontype-parameter>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<type>bool</type>
|
||||||
|
|
||||||
|
<parameter name="x">
|
||||||
|
<paramtype>const <classname>array</classname><T, N>&</paramtype>
|
||||||
|
</parameter>
|
||||||
|
<parameter name="y">
|
||||||
|
<paramtype>const <classname>array</classname><T, N>&</paramtype>
|
||||||
|
</parameter>
|
||||||
|
|
||||||
|
<returns><simpara><code>std::equal(x.<methodname>begin</methodname>(), x.<methodname>end</methodname>(), y.<methodname>begin</methodname>())</code></simpara>
|
||||||
|
</returns>
|
||||||
|
</function>
|
||||||
|
|
||||||
|
<function name="operator!=">
|
||||||
|
<template>
|
||||||
|
<template-type-parameter name="T"/>
|
||||||
|
<template-nontype-parameter name="N">
|
||||||
|
<type>std::size_t</type>
|
||||||
|
</template-nontype-parameter>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<type>bool</type>
|
||||||
|
|
||||||
|
<parameter name="x">
|
||||||
|
<paramtype>const <classname>array</classname><T, N>&</paramtype>
|
||||||
|
</parameter>
|
||||||
|
<parameter name="y">
|
||||||
|
<paramtype>const <classname>array</classname><T, N>&</paramtype>
|
||||||
|
</parameter>
|
||||||
|
|
||||||
|
<returns><simpara><code>!(x == y)</code></simpara>
|
||||||
|
</returns>
|
||||||
|
</function>
|
||||||
|
|
||||||
|
<function name="operator<">
|
||||||
|
<template>
|
||||||
|
<template-type-parameter name="T"/>
|
||||||
|
<template-nontype-parameter name="N">
|
||||||
|
<type>std::size_t</type>
|
||||||
|
</template-nontype-parameter>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<type>bool</type>
|
||||||
|
|
||||||
|
<parameter name="x">
|
||||||
|
<paramtype>const <classname>array</classname><T, N>&</paramtype>
|
||||||
|
</parameter>
|
||||||
|
<parameter name="y">
|
||||||
|
<paramtype>const <classname>array</classname><T, N>&</paramtype>
|
||||||
|
</parameter>
|
||||||
|
|
||||||
|
<returns><simpara><code>std::lexicographical_compare(x.<methodname>begin</methodname>(), x.<methodname>end</methodname>(), y.<methodname>begin</methodname>(), y.<methodname>end</methodname>())</code></simpara>
|
||||||
|
</returns>
|
||||||
|
</function>
|
||||||
|
|
||||||
|
<function name="operator>">
|
||||||
|
<template>
|
||||||
|
<template-type-parameter name="T"/>
|
||||||
|
<template-nontype-parameter name="N">
|
||||||
|
<type>std::size_t</type>
|
||||||
|
</template-nontype-parameter>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<type>bool</type>
|
||||||
|
|
||||||
|
<parameter name="x">
|
||||||
|
<paramtype>const <classname>array</classname><T, N>&</paramtype>
|
||||||
|
</parameter>
|
||||||
|
<parameter name="y">
|
||||||
|
<paramtype>const <classname>array</classname><T, N>&</paramtype>
|
||||||
|
</parameter>
|
||||||
|
|
||||||
|
<returns><simpara><code>y < x</code></simpara></returns>
|
||||||
|
</function>
|
||||||
|
|
||||||
|
<function name="operator<=">
|
||||||
|
<template>
|
||||||
|
<template-type-parameter name="T"/>
|
||||||
|
<template-nontype-parameter name="N">
|
||||||
|
<type>std::size_t</type>
|
||||||
|
</template-nontype-parameter>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<type>bool</type>
|
||||||
|
|
||||||
|
<parameter name="x">
|
||||||
|
<paramtype>const <classname>array</classname><T, N>&</paramtype>
|
||||||
|
</parameter>
|
||||||
|
<parameter name="y">
|
||||||
|
<paramtype>const <classname>array</classname><T, N>&</paramtype>
|
||||||
|
</parameter>
|
||||||
|
|
||||||
|
<returns><simpara><code>!(y < x)</code></simpara></returns>
|
||||||
|
</function>
|
||||||
|
|
||||||
|
<function name="operator>=">
|
||||||
|
<template>
|
||||||
|
<template-type-parameter name="T"/>
|
||||||
|
<template-nontype-parameter name="N">
|
||||||
|
<type>std::size_t</type>
|
||||||
|
</template-nontype-parameter>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<type>bool</type>
|
||||||
|
|
||||||
|
<parameter name="x">
|
||||||
|
<paramtype>const <classname>array</classname><T, N>&</paramtype>
|
||||||
|
</parameter>
|
||||||
|
<parameter name="y">
|
||||||
|
<paramtype>const <classname>array</classname><T, N>&</paramtype>
|
||||||
|
</parameter>
|
||||||
|
|
||||||
|
<returns><simpara><code>!(x < y)</code></simpara></returns>
|
||||||
|
</function>
|
||||||
|
</free-function-group>
|
||||||
|
</class>
|
||||||
|
</namespace>
|
||||||
|
</header>
|
||||||
|
</library-reference>
|
||||||
|
|
||||||
|
<section id="array.rationale">
|
||||||
|
<title>Design Rationale</title>
|
||||||
|
|
||||||
|
<para>There was an important design tradeoff regarding the
|
||||||
|
constructors: We could implement array as an "aggregate" (see
|
||||||
|
Section 8.5.1, [dcl.init.aggr], of the C++ Standard). This would
|
||||||
|
mean:
|
||||||
|
<itemizedlist>
|
||||||
|
<listitem><simpara>An array can be initialized with a
|
||||||
|
brace-enclosing, comma-separated list of initializers for the
|
||||||
|
elements of the container, written in increasing subscript
|
||||||
|
order:</simpara>
|
||||||
|
|
||||||
|
<programlisting><classname>boost::array</classname><int,4> a = { { 1, 2, 3 } };</programlisting>
|
||||||
|
|
||||||
|
<simpara>Note that if there are fewer elements in the
|
||||||
|
initializer list, then each remaining element gets
|
||||||
|
default-initialized (thus, it has a defined value).</simpara>
|
||||||
|
</listitem></itemizedlist></para>
|
||||||
|
|
||||||
|
<para>However, this approach has its drawbacks: <emphasis
|
||||||
|
role="bold"> passing no initializer list means that the elements
|
||||||
|
have an indetermined initial value</emphasis>, because the rule says
|
||||||
|
that aggregates may have:
|
||||||
|
<itemizedlist>
|
||||||
|
<listitem><simpara>No user-declared constructors.</simpara></listitem>
|
||||||
|
<listitem><simpara>No private or protected non-static data members.</simpara></listitem>
|
||||||
|
<listitem><simpara>No base classes.</simpara></listitem>
|
||||||
|
<listitem><simpara>No virtual functions.</simpara></listitem>
|
||||||
|
</itemizedlist>
|
||||||
|
</para>
|
||||||
|
|
||||||
|
<para>Nevertheless, The current implementation uses this approach.</para>
|
||||||
|
|
||||||
|
<para>Note that for standard conforming compilers it is possible to
|
||||||
|
use fewer braces (according to 8.5.1 (11) of the Standard). That is,
|
||||||
|
you can initialize an array as follows:</para>
|
||||||
|
|
||||||
|
<programlisting>
|
||||||
|
<classname>boost::array</classname><int,4> a = { 1, 2, 3 };
|
||||||
|
</programlisting>
|
||||||
|
|
||||||
|
<para>I'd appreciate any constructive feedback. <emphasis
|
||||||
|
role="bold">Please note: I don't have time to read all boost
|
||||||
|
mails. Thus, to make sure that feedback arrives to me, please send
|
||||||
|
me a copy of each mail regarding this class.</emphasis></para>
|
||||||
|
|
||||||
|
<para>The code is provided "as is" without expressed or implied
|
||||||
|
warranty.</para>
|
||||||
|
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section id="array.more.info">
|
||||||
|
<title>For more information...</title>
|
||||||
|
<para>To find more details about using ordinary arrays in C++ and
|
||||||
|
the framework of the STL, see e.g.
|
||||||
|
|
||||||
|
<literallayout>The C++ Standard Library - A Tutorial and Reference
|
||||||
|
by Nicolai M. Josuttis
|
||||||
|
Addison Wesley Longman, 1999
|
||||||
|
ISBN 0-201-37926-0</literallayout>
|
||||||
|
</para>
|
||||||
|
|
||||||
|
<para><ulink url="http://www.josuttis.com/">Home Page of Nicolai
|
||||||
|
Josuttis</ulink></para>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section id="array.ack">
|
||||||
|
<title>Acknowledgements</title>
|
||||||
|
|
||||||
|
<para>Doug Gregor ported the documentation to the BoostBook format.</para>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<!-- Notes:
|
||||||
|
empty() should return N != 0
|
||||||
|
size(), empty(), max_size() should be const
|
||||||
|
-->
|
||||||
|
|
||||||
|
</library>
|
@ -2,17 +2,16 @@
|
|||||||
* an STL container (as wrapper) for arrays of constant size.
|
* an STL container (as wrapper) for arrays of constant size.
|
||||||
*
|
*
|
||||||
* See
|
* See
|
||||||
* http://www.josuttis.com/cppcode
|
* http://www.boost.org/libs/array/
|
||||||
* for details and the latest version.
|
|
||||||
* See
|
|
||||||
* http://www.boost.org/libs/array for Documentation.
|
|
||||||
* for documentation.
|
* for documentation.
|
||||||
*
|
*
|
||||||
|
* The original author site is at: http://www.josuttis.com/
|
||||||
|
*
|
||||||
* (C) Copyright Nicolai M. Josuttis 2001.
|
* (C) Copyright Nicolai M. Josuttis 2001.
|
||||||
* Permission to copy, use, modify, sell and distribute this software
|
*
|
||||||
* is granted provided this copyright notice appears in all copies.
|
* Distributed under the Boost Software License, Version 1.0. (See
|
||||||
* This software is provided "as is" without express or implied
|
* accompanying file LICENSE_1_0.txt or copy at
|
||||||
* warranty, and with no claim as to its suitability for any purpose.
|
* http://www.boost.org/LICENSE_1_0.txt)
|
||||||
*
|
*
|
||||||
* 29 Jan 2004 - c_array() added, BOOST_NO_PRIVATE_IN_AGGREGATE removed (Nico Josuttis)
|
* 29 Jan 2004 - c_array() added, BOOST_NO_PRIVATE_IN_AGGREGATE removed (Nico Josuttis)
|
||||||
* 23 Aug 2002 - fix for Non-MSVC compilers combined with MSVC libraries.
|
* 23 Aug 2002 - fix for Non-MSVC compilers combined with MSVC libraries.
|
||||||
@ -27,9 +26,11 @@
|
|||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
#include <boost/assert.hpp>
|
||||||
|
|
||||||
// Handles broken standard libraries better than <iterator>
|
// Handles broken standard libraries better than <iterator>
|
||||||
#include <boost/detail/iterator.hpp>
|
#include <boost/detail/iterator.hpp>
|
||||||
|
#include <boost/throw_exception.hpp>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
// FIXES for broken compilers
|
// FIXES for broken compilers
|
||||||
@ -52,7 +53,7 @@ namespace boost {
|
|||||||
typedef const T& const_reference;
|
typedef const T& const_reference;
|
||||||
typedef std::size_t size_type;
|
typedef std::size_t size_type;
|
||||||
typedef std::ptrdiff_t difference_type;
|
typedef std::ptrdiff_t difference_type;
|
||||||
|
|
||||||
// iterator support
|
// iterator support
|
||||||
iterator begin() { return elems; }
|
iterator begin() { return elems; }
|
||||||
const_iterator begin() const { return elems; }
|
const_iterator begin() const { return elems; }
|
||||||
@ -85,18 +86,42 @@ namespace boost {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// operator[]
|
// operator[]
|
||||||
reference operator[](size_type i) { return elems[i]; }
|
reference operator[](size_type i)
|
||||||
const_reference operator[](size_type i) const { return elems[i]; }
|
{
|
||||||
|
BOOST_ASSERT( i < N && "out of range" );
|
||||||
|
return elems[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
const_reference operator[](size_type i) const
|
||||||
|
{
|
||||||
|
BOOST_ASSERT( i < N && "out of range" );
|
||||||
|
return elems[i];
|
||||||
|
}
|
||||||
|
|
||||||
// at() with range check
|
// at() with range check
|
||||||
reference at(size_type i) { rangecheck(i); return elems[i]; }
|
reference at(size_type i) { rangecheck(i); return elems[i]; }
|
||||||
const_reference at(size_type i) const { rangecheck(i); return elems[i]; }
|
const_reference at(size_type i) const { rangecheck(i); return elems[i]; }
|
||||||
|
|
||||||
// front() and back()
|
// front() and back()
|
||||||
reference front() { return elems[0]; }
|
reference front()
|
||||||
const_reference front() const { return elems[0]; }
|
{
|
||||||
reference back() { return elems[N-1]; }
|
return elems[0];
|
||||||
const_reference back() const { return elems[N-1]; }
|
}
|
||||||
|
|
||||||
|
const_reference front() const
|
||||||
|
{
|
||||||
|
return elems[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
reference back()
|
||||||
|
{
|
||||||
|
return elems[N-1];
|
||||||
|
}
|
||||||
|
|
||||||
|
const_reference back() const
|
||||||
|
{
|
||||||
|
return elems[N-1];
|
||||||
|
}
|
||||||
|
|
||||||
// size is constant
|
// size is constant
|
||||||
static size_type size() { return N; }
|
static size_type size() { return N; }
|
||||||
@ -111,6 +136,7 @@ namespace boost {
|
|||||||
|
|
||||||
// direct access to data (read-only)
|
// direct access to data (read-only)
|
||||||
const T* data() const { return elems; }
|
const T* data() const { return elems; }
|
||||||
|
T* data() { return elems; }
|
||||||
|
|
||||||
// use array as C array (direct read/write access to data)
|
// use array as C array (direct read/write access to data)
|
||||||
T* c_array() { return elems; }
|
T* c_array() { return elems; }
|
||||||
@ -130,13 +156,134 @@ namespace boost {
|
|||||||
|
|
||||||
// check range (may be private because it is static)
|
// check range (may be private because it is static)
|
||||||
static void rangecheck (size_type i) {
|
static void rangecheck (size_type i) {
|
||||||
if (i >= size()) {
|
if (i >= size()) {
|
||||||
throw std::range_error("array<>: index out of range");
|
throw std::out_of_range("array<>: index out of range");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
|
||||||
|
template< class T >
|
||||||
|
class array< T, 0 > {
|
||||||
|
|
||||||
|
public:
|
||||||
|
// type definitions
|
||||||
|
typedef T value_type;
|
||||||
|
typedef T* iterator;
|
||||||
|
typedef const T* const_iterator;
|
||||||
|
typedef T& reference;
|
||||||
|
typedef const T& const_reference;
|
||||||
|
typedef std::size_t size_type;
|
||||||
|
typedef std::ptrdiff_t difference_type;
|
||||||
|
|
||||||
|
// iterator support
|
||||||
|
iterator begin() { return iterator( reinterpret_cast< T * >( this ) ); }
|
||||||
|
const_iterator begin() const { return const_iterator( reinterpret_cast< const T * >( this ) ); }
|
||||||
|
iterator end() { return begin(); }
|
||||||
|
const_iterator end() const { return begin(); }
|
||||||
|
|
||||||
|
// reverse iterator support
|
||||||
|
#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS)
|
||||||
|
typedef std::reverse_iterator<iterator> reverse_iterator;
|
||||||
|
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
|
||||||
|
#elif defined(_MSC_VER) && (_MSC_VER == 1300) && defined(BOOST_DINKUMWARE_STDLIB) && (BOOST_DINKUMWARE_STDLIB == 310)
|
||||||
|
// workaround for broken reverse_iterator in VC7
|
||||||
|
typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, iterator,
|
||||||
|
reference, iterator, reference> > reverse_iterator;
|
||||||
|
typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, const_iterator,
|
||||||
|
const_reference, iterator, reference> > const_reverse_iterator;
|
||||||
|
#else
|
||||||
|
// workaround for broken reverse_iterator implementations
|
||||||
|
typedef std::reverse_iterator<iterator,T> reverse_iterator;
|
||||||
|
typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
reverse_iterator rbegin() { return reverse_iterator(end()); }
|
||||||
|
const_reverse_iterator rbegin() const {
|
||||||
|
return const_reverse_iterator(end());
|
||||||
|
}
|
||||||
|
reverse_iterator rend() { return reverse_iterator(begin()); }
|
||||||
|
const_reverse_iterator rend() const {
|
||||||
|
return const_reverse_iterator(begin());
|
||||||
|
}
|
||||||
|
|
||||||
|
// operator[]
|
||||||
|
reference operator[](size_type i)
|
||||||
|
{
|
||||||
|
return failed_rangecheck();
|
||||||
|
}
|
||||||
|
|
||||||
|
const_reference operator[](size_type i) const
|
||||||
|
{
|
||||||
|
return failed_rangecheck();
|
||||||
|
}
|
||||||
|
|
||||||
|
// at() with range check
|
||||||
|
reference at(size_type i) { return failed_rangecheck(); }
|
||||||
|
const_reference at(size_type i) const { return failed_rangecheck(); }
|
||||||
|
|
||||||
|
// front() and back()
|
||||||
|
reference front()
|
||||||
|
{
|
||||||
|
return failed_rangecheck();
|
||||||
|
}
|
||||||
|
|
||||||
|
const_reference front() const
|
||||||
|
{
|
||||||
|
return failed_rangecheck();
|
||||||
|
}
|
||||||
|
|
||||||
|
reference back()
|
||||||
|
{
|
||||||
|
return failed_rangecheck();
|
||||||
|
}
|
||||||
|
|
||||||
|
const_reference back() const
|
||||||
|
{
|
||||||
|
return failed_rangecheck();
|
||||||
|
}
|
||||||
|
|
||||||
|
// size is constant
|
||||||
|
static size_type size() { return 0; }
|
||||||
|
static bool empty() { return true; }
|
||||||
|
static size_type max_size() { return 0; }
|
||||||
|
enum { static_size = 0 };
|
||||||
|
|
||||||
|
void swap (array<T,0>& y) {
|
||||||
|
}
|
||||||
|
|
||||||
|
// direct access to data (read-only)
|
||||||
|
const T* data() const { return 0; }
|
||||||
|
T* data() { return 0; }
|
||||||
|
|
||||||
|
// use array as C array (direct read/write access to data)
|
||||||
|
T* c_array() { return 0; }
|
||||||
|
|
||||||
|
// assignment with type conversion
|
||||||
|
template <typename T2>
|
||||||
|
array<T,0>& operator= (const array<T2,0>& ) {
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// assign one value to all elements
|
||||||
|
void assign (const T& ) { }
|
||||||
|
|
||||||
|
// check range (may be private because it is static)
|
||||||
|
static reference failed_rangecheck () {
|
||||||
|
std::out_of_range e("attempt to access element of an empty array");
|
||||||
|
boost::throw_exception(e);
|
||||||
|
//
|
||||||
|
// We need to return something here to keep
|
||||||
|
// some compilers happy: however we will never
|
||||||
|
// actually get here....
|
||||||
|
//
|
||||||
|
static T placeholder;
|
||||||
|
return placeholder;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
// comparisons
|
// comparisons
|
||||||
template<class T, std::size_t N>
|
template<class T, std::size_t N>
|
||||||
bool operator== (const array<T,N>& x, const array<T,N>& y) {
|
bool operator== (const array<T,N>& x, const array<T,N>& y) {
|
||||||
|
13
index.html
Normal file
13
index.html
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="refresh" content="0; URL=../../doc/html/array.html">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
Automatic redirection failed, please go to
|
||||||
|
<a href="../../doc/html/array.html">../../doc/html/array.html</a> <hr>
|
||||||
|
<p><EFBFBD> Copyright Beman Dawes, 2001</p>
|
||||||
|
<p>Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or copy
|
||||||
|
at <a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a>)</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
14
test/Jamfile.v2
Normal file
14
test/Jamfile.v2
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#~ Copyright Rene Rivera 2008
|
||||||
|
#~ Distributed under the Boost Software License, Version 1.0.
|
||||||
|
#~ (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
|
||||||
|
import testing ;
|
||||||
|
|
||||||
|
test-suite array :
|
||||||
|
[ run array0.cpp ]
|
||||||
|
[ run array1.cpp ]
|
||||||
|
[ run array2.cpp ]
|
||||||
|
[ run array3.cpp ]
|
||||||
|
[ run array4.cpp ]
|
||||||
|
[ run array5.cpp ]
|
||||||
|
;
|
107
test/array0.cpp
Normal file
107
test/array0.cpp
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
/* tests for using class array<> specialization for size 0
|
||||||
|
* (C) Copyright Alisdair Meredith 2006.
|
||||||
|
* Distributed under the Boost Software License, Version 1.0. (See
|
||||||
|
* accompanying file LICENSE_1_0.txt or copy at
|
||||||
|
* http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include <boost/array.hpp>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
unsigned int failed_tests = 0;
|
||||||
|
|
||||||
|
void fail_test( const char * reason ) {
|
||||||
|
++failed_tests;
|
||||||
|
std::cerr << "Test failure " << failed_tests << ": " << reason << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
template< class T >
|
||||||
|
void BadValue( const T & )
|
||||||
|
{
|
||||||
|
fail_test( "Unexpected value" );
|
||||||
|
}
|
||||||
|
|
||||||
|
template< class T >
|
||||||
|
void RunTests()
|
||||||
|
{
|
||||||
|
typedef boost::array< T, 0 > test_type;
|
||||||
|
|
||||||
|
// Test value and aggegrate initialization
|
||||||
|
test_type test_case = {};
|
||||||
|
const boost::array< T, 0 > const_test_case = test_type();
|
||||||
|
|
||||||
|
test_case.assign( T() );
|
||||||
|
|
||||||
|
// front/back and operator[] must compile, but calling them is undefined
|
||||||
|
// Likewise, all tests below should evaluate to false, avoiding undefined behaviour
|
||||||
|
if( !test_case.empty() ) {
|
||||||
|
BadValue( test_case.front() );
|
||||||
|
}
|
||||||
|
|
||||||
|
if( !const_test_case.empty() ) {
|
||||||
|
BadValue( const_test_case.back() );
|
||||||
|
}
|
||||||
|
|
||||||
|
if( test_case.size() > 0 ) {
|
||||||
|
BadValue( test_case[ 0 ] );
|
||||||
|
}
|
||||||
|
|
||||||
|
if( const_test_case.max_size() > 0 ) {
|
||||||
|
BadValue( const_test_case[ 0 ] );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assert requirements of TR1 6.2.2.4
|
||||||
|
if( test_case.begin() != test_case.end() ) {
|
||||||
|
fail_test( "Not an empty range" );
|
||||||
|
}
|
||||||
|
if( const_test_case.begin() != const_test_case.end() ) {
|
||||||
|
fail_test( "Not an empty range" );
|
||||||
|
}
|
||||||
|
|
||||||
|
if( test_case.begin() == const_test_case.begin() ) {
|
||||||
|
fail_test( "iterators for different containers are not distinct" );
|
||||||
|
}
|
||||||
|
|
||||||
|
if( test_case.data() == const_test_case.data() ) {
|
||||||
|
// Value of data is unspecified in TR1, so no requirement this test pass or fail
|
||||||
|
// However, it must compile!
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Check can safely use all iterator types with std algorithms
|
||||||
|
std::for_each( test_case.begin(), test_case.end(), BadValue< T > );
|
||||||
|
std::for_each( test_case.rbegin(), test_case.rend(), BadValue< T > );
|
||||||
|
std::for_each( const_test_case.begin(), const_test_case.end(), BadValue< T > );
|
||||||
|
std::for_each( const_test_case.rbegin(), const_test_case.rend(), BadValue< T > );
|
||||||
|
|
||||||
|
// Check swap is well formed
|
||||||
|
std::swap( test_case, test_case );
|
||||||
|
|
||||||
|
// Check assigment operator and overloads are well formed
|
||||||
|
test_case = const_test_case;
|
||||||
|
|
||||||
|
// Confirm at() throws the std lib defined exception
|
||||||
|
try {
|
||||||
|
BadValue( test_case.at( 0 ) );
|
||||||
|
} catch ( const std::out_of_range & ) {
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
BadValue( const_test_case.at( 0 ) );
|
||||||
|
} catch ( const std::out_of_range & ) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
RunTests< bool >();
|
||||||
|
RunTests< void * >();
|
||||||
|
RunTests< long double >();
|
||||||
|
RunTests< std::string >();
|
||||||
|
return failed_tests;
|
||||||
|
}
|
||||||
|
|
58
test/array1.cpp
Normal file
58
test/array1.cpp
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
/* simple example for using class array<>
|
||||||
|
*
|
||||||
|
* (C) Copyright Nicolai M. Josuttis 2001.
|
||||||
|
* Distributed under the Boost Software License, Version 1.0. (See
|
||||||
|
* accompanying file LICENSE_1_0.txt or copy at
|
||||||
|
* http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
*
|
||||||
|
* Changelog:
|
||||||
|
* 20 Jan 2001 - Removed boolalpha use since stock GCC doesn't support it
|
||||||
|
* (David Abrahams)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <boost/array.hpp>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// define special type name
|
||||||
|
typedef boost::array<float,6> Array;
|
||||||
|
|
||||||
|
// create and initialize an array
|
||||||
|
Array a = { { 42 } };
|
||||||
|
|
||||||
|
// access elements
|
||||||
|
for (unsigned i=1; i<a.size(); ++i) {
|
||||||
|
a[i] = a[i-1]+1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// use some common STL container operations
|
||||||
|
std::cout << "size: " << a.size() << std::endl;
|
||||||
|
std::cout << "empty: " << (a.empty() ? "true" : "false") << std::endl;
|
||||||
|
std::cout << "max_size: " << a.max_size() << std::endl;
|
||||||
|
std::cout << "front: " << a.front() << std::endl;
|
||||||
|
std::cout << "back: " << a.back() << std::endl;
|
||||||
|
std::cout << "elems: ";
|
||||||
|
|
||||||
|
// iterate through all elements
|
||||||
|
for (Array::const_iterator pos=a.begin(); pos<a.end(); ++pos) {
|
||||||
|
std::cout << *pos << ' ';
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
// check copy constructor and assignment operator
|
||||||
|
Array b(a);
|
||||||
|
Array c;
|
||||||
|
c = a;
|
||||||
|
if (a==b && a==c) {
|
||||||
|
std::cout << "copy construction and copy assignment are OK"
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
std::cout << "copy construction and copy assignment FAILED"
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0; // makes Visual-C++ compiler happy
|
||||||
|
}
|
||||||
|
|
40
test/array2.cpp
Normal file
40
test/array2.cpp
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
/* example for using class array<>
|
||||||
|
* (C) Copyright Nicolai M. Josuttis 2001.
|
||||||
|
* Distributed under the Boost Software License, Version 1.0. (See
|
||||||
|
* accompanying file LICENSE_1_0.txt or copy at
|
||||||
|
* http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <functional>
|
||||||
|
#include <boost/array.hpp>
|
||||||
|
#include "print.hpp"
|
||||||
|
using namespace std;
|
||||||
|
using namespace boost;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// create and initialize array
|
||||||
|
array<int,10> a = { { 1, 2, 3, 4, 5 } };
|
||||||
|
|
||||||
|
print_elements(a);
|
||||||
|
|
||||||
|
// modify elements directly
|
||||||
|
for (unsigned i=0; i<a.size(); ++i) {
|
||||||
|
++a[i];
|
||||||
|
}
|
||||||
|
print_elements(a);
|
||||||
|
|
||||||
|
// change order using an STL algorithm
|
||||||
|
reverse(a.begin(),a.end());
|
||||||
|
print_elements(a);
|
||||||
|
|
||||||
|
// negate elements using STL framework
|
||||||
|
transform(a.begin(),a.end(), // source
|
||||||
|
a.begin(), // destination
|
||||||
|
negate<int>()); // operation
|
||||||
|
print_elements(a);
|
||||||
|
|
||||||
|
return 0; // makes Visual-C++ compiler happy
|
||||||
|
}
|
||||||
|
|
55
test/array3.cpp
Normal file
55
test/array3.cpp
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
/* example for using class array<>
|
||||||
|
* (C) Copyright Nicolai M. Josuttis 2001.
|
||||||
|
* Distributed under the Boost Software License, Version 1.0. (See
|
||||||
|
* accompanying file LICENSE_1_0.txt or copy at
|
||||||
|
* http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include <boost/array.hpp>
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void print_elements (const T& x);
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// create array of four seasons
|
||||||
|
boost::array<std::string,4> seasons = {
|
||||||
|
{ "spring", "summer", "autumn", "winter" }
|
||||||
|
};
|
||||||
|
|
||||||
|
// copy and change order
|
||||||
|
boost::array<std::string,4> seasons_orig = seasons;
|
||||||
|
for (unsigned i=seasons.size()-1; i>0; --i) {
|
||||||
|
std::swap(seasons.at(i),seasons.at((i+1)%seasons.size()));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "one way: ";
|
||||||
|
print_elements(seasons);
|
||||||
|
|
||||||
|
// try swap()
|
||||||
|
std::cout << "other way: ";
|
||||||
|
std::swap(seasons,seasons_orig);
|
||||||
|
print_elements(seasons);
|
||||||
|
|
||||||
|
// try reverse iterators
|
||||||
|
std::cout << "reverse: ";
|
||||||
|
for (boost::array<std::string,4>::reverse_iterator pos
|
||||||
|
=seasons.rbegin(); pos<seasons.rend(); ++pos) {
|
||||||
|
std::cout << " " << *pos;
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
return 0; // makes Visual-C++ compiler happy
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void print_elements (const T& x)
|
||||||
|
{
|
||||||
|
for (unsigned i=0; i<x.size(); ++i) {
|
||||||
|
std::cout << " " << x[i];
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
|
43
test/array4.cpp
Normal file
43
test/array4.cpp
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/* example for using class array<>
|
||||||
|
* (C) Copyright Nicolai M. Josuttis 2001.
|
||||||
|
* Distributed under the Boost Software License, Version 1.0. (See
|
||||||
|
* accompanying file LICENSE_1_0.txt or copy at
|
||||||
|
* http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <functional>
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include <boost/array.hpp>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// array of arrays of seasons
|
||||||
|
boost::array<boost::array<std::string,4>,2> seasons_i18n = {
|
||||||
|
{ { { "spring", "summer", "autumn", "winter", } },
|
||||||
|
{ { "Fruehling", "Sommer", "Herbst", "Winter" } }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// for any array of seasons print seasons
|
||||||
|
for (unsigned i=0; i<seasons_i18n.size(); ++i) {
|
||||||
|
boost::array<std::string,4> seasons = seasons_i18n[i];
|
||||||
|
for (unsigned j=0; j<seasons.size(); ++j) {
|
||||||
|
std::cout << seasons[j] << " ";
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// print first element of first array
|
||||||
|
std::cout << "first element of first array: "
|
||||||
|
<< seasons_i18n[0][0] << std::endl;
|
||||||
|
|
||||||
|
// print last element of last array
|
||||||
|
std::cout << "last element of last array: "
|
||||||
|
<< seasons_i18n[seasons_i18n.size()-1][seasons_i18n[0].size()-1]
|
||||||
|
<< std::endl;
|
||||||
|
|
||||||
|
return 0; // makes Visual-C++ compiler happy
|
||||||
|
}
|
||||||
|
|
72
test/array5.cpp
Normal file
72
test/array5.cpp
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
/* simple example for using class array<>
|
||||||
|
* (C) Copyright Nicolai M. Josuttis 2001.
|
||||||
|
* Distributed under the Boost Software License, Version 1.0. (See
|
||||||
|
* accompanying file LICENSE_1_0.txt or copy at
|
||||||
|
* http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <boost/array.hpp>
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void test_static_size (const T& cont)
|
||||||
|
{
|
||||||
|
int tmp[T::static_size];
|
||||||
|
for (unsigned i=0; i<T::static_size; ++i) {
|
||||||
|
tmp[i] = int(cont[i]);
|
||||||
|
}
|
||||||
|
for (unsigned j=0; j<T::static_size; ++j) {
|
||||||
|
std::cout << tmp[j] << ' ';
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// define special type name
|
||||||
|
typedef boost::array<float,6> Array;
|
||||||
|
|
||||||
|
// create and initialize an array
|
||||||
|
const Array a = { { 42.42 } };
|
||||||
|
|
||||||
|
// use some common STL container operations
|
||||||
|
std::cout << "static_size: " << a.size() << std::endl;
|
||||||
|
std::cout << "size: " << a.size() << std::endl;
|
||||||
|
// Can't use std::boolalpha because it isn't portable
|
||||||
|
std::cout << "empty: " << (a.empty()? "true" : "false") << std::endl;
|
||||||
|
std::cout << "max_size: " << a.max_size() << std::endl;
|
||||||
|
std::cout << "front: " << a.front() << std::endl;
|
||||||
|
std::cout << "back: " << a.back() << std::endl;
|
||||||
|
std::cout << "[0]: " << a[0] << std::endl;
|
||||||
|
std::cout << "elems: ";
|
||||||
|
|
||||||
|
// iterate through all elements
|
||||||
|
for (Array::const_iterator pos=a.begin(); pos<a.end(); ++pos) {
|
||||||
|
std::cout << *pos << ' ';
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
test_static_size(a);
|
||||||
|
|
||||||
|
// check copy constructor and assignment operator
|
||||||
|
Array b(a);
|
||||||
|
Array c;
|
||||||
|
c = a;
|
||||||
|
if (a==b && a==c) {
|
||||||
|
std::cout << "copy construction and copy assignment are OK"
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
std::cout << "copy construction and copy assignment are BROKEN"
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef boost::array<double,6> DArray;
|
||||||
|
typedef boost::array<int,6> IArray;
|
||||||
|
IArray ia = { { 1, 2, 3, 4, 5, 6 } } ; // extra braces silence GCC warning
|
||||||
|
DArray da;
|
||||||
|
da = ia;
|
||||||
|
da.assign(42);
|
||||||
|
|
||||||
|
return 0; // makes Visual-C++ compiler happy
|
||||||
|
}
|
||||||
|
|
27
test/print.hpp
Normal file
27
test/print.hpp
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
/* The following code example is taken from the book
|
||||||
|
* "The C++ Standard Library - A Tutorial and Reference"
|
||||||
|
* by Nicolai M. Josuttis, Addison-Wesley, 1999
|
||||||
|
*
|
||||||
|
* (C) Copyright Nicolai M. Josuttis 1999.
|
||||||
|
* Distributed under the Boost Software License, Version 1.0. (See
|
||||||
|
* accompanying file LICENSE_1_0.txt or copy at
|
||||||
|
* http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
*/
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
/* print_elements()
|
||||||
|
* - prints optional C-string optcstr followed by
|
||||||
|
* - all elements of the collection coll
|
||||||
|
* - separated by spaces
|
||||||
|
*/
|
||||||
|
template <class T>
|
||||||
|
inline void print_elements (const T& coll, const char* optcstr="")
|
||||||
|
{
|
||||||
|
typename T::const_iterator pos;
|
||||||
|
|
||||||
|
std::cout << optcstr;
|
||||||
|
for (pos=coll.begin(); pos!=coll.end(); ++pos) {
|
||||||
|
std::cout << *pos << ' ';
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
Reference in New Issue
Block a user