File: array.html

package info (click to toggle)
boost 1.27.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 19,908 kB
  • ctags: 26,546
  • sloc: cpp: 122,225; ansic: 10,956; python: 4,412; sh: 855; yacc: 803; makefile: 257; perl: 165; lex: 90; csh: 6
file content (330 lines) | stat: -rw-r--r-- 18,875 bytes parent folder | download
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Keywords" content="array, block, carray, c_array, array wrapper, adapter, adaptor, STL, C++ Standard Library, array.hpp">
   
<title>array.hpp, an STL Array Wrapper</title>
</head>
<body text="#000000" bgcolor="#FFFFFF" link="#186ABF">
<font face="Arial, Helvetica, sans-serif">&nbsp; </font> 
<table width="100%" height="40">
  <tr> 
    <td BGCOLOR="#DDDDDD"><b><font face="Arial,helvetica" color="#000000" size="+1">Class 
      <font face="Courier New, Courier, mono">array</font>, an STL Container (as 
      Wrapper) for Arrays of Constant Size</font></b></td>
  </tr>
</table>
<p><font size="-1" face="Arial, Helvetica, sans-serif">[<a href="#intro">intro</a>] 
  [<a href="#interface">interface</a>] [<a href="#discussion">discussion</a>] 
  [<a href="#code">code</a>]</font>
<p><font face="Arial, Helvetica, sans-serif" size="-1"><a name="intro"></a>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).</font> 
<p><font face="Arial, Helvetica, sans-serif" size="-1">As replacement for ordinary 
  arrays, the STL provides class <font face="Courier New, Courier, mono">vector&lt;&gt;</font>. 
  However, <font face="Courier New, Courier, mono">vector&lt;&gt;</font> 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.</font> 
<p><font face="Arial, Helvetica, sans-serif" size="-1">In his book, <i>Generic 
  Programming and the STL</i>, Matthew H. Austern introduces a useful wrapper 
  class for ordinary arrays with static size, called <font face="Courier New, Courier, mono"><b>block</b></font>. 
  It is safer and has no worse performance than ordinary arrays. In <i>The C++ 
  Programming Language</i>, 3rd edition, Bjarne Stroustrup introduces a similar 
  class, called <font face="Courier New, Courier, mono"><b>c_array</b></font>, 
  which I (<a href="http://www.josuttis.com">Nicolai Josuttis</a>) present slightly 
  modified in my book <i>The C++ Standard Library - A Tutorial and Reference</i>, 
  called <font face="Courier New, Courier, mono"><b>carray</b></font>. This is 
  the essence of these approaches spiced with many feedback from <a href="http://www.boost.org">boost</a>.</font> 
<p><font face="Arial, Helvetica, sans-serif" size="-1">After considering different 
  names, we decided to name this class simply <font face="Courier New, Courier, mono"><b>array</b></font>.</font> 
<p><font face="Arial, Helvetica, sans-serif" size="-1"><a name="interface"></a>The 
  class provides the following interface:</font> 
<table border="0">
  <tr> 
    <td><font face="Arial, Helvetica, sans-serif" size="-1"><b>Types:</b></font></td>
    <td><font size="-1"></font></td>
  </tr>
  <tr> 
    <td><font face="Courier New, Courier, mono" size="-1">value_type</font></td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">type of the elements</font></td>
  </tr>
  <tr> 
    <td><font face="Courier New, Courier, mono" size="-1">iterator</font></td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">type of the iterator 
      (random-access iterator)</font></td>
  </tr>
  <tr> 
    <td><font face="Courier New, Courier, mono" size="-1">const_iterator</font></td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">type of iterator that 
      considers elements as being constant</font></td>
  </tr>
  <tr> 
    <td><font face="Courier New, Courier, mono" size="-1">reference</font></td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">type of element reference</font></td>
  </tr>
  <tr> 
    <td><font face="Courier New, Courier, mono" size="-1">const_reference</font></td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">type of element reference 
      that considers elements as being constant</font></td>
  </tr>
  <tr> 
    <td><font face="Courier New, Courier, mono" size="-1">size_type</font></td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">type for signed size 
      values</font></td>
  </tr>
  <tr> 
    <td><font face="Courier New, Courier, mono" size="-1">difference_type</font></td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">type for unsigned 
      difference values</font></td>
  </tr>
  <tr> 
    <td><font face="Arial, Helvetica, sans-serif" size="-1"><b>Operations:</b></font></td>
    <td><font size="-1"></font></td>
  </tr>
  <tr> 
    <td> 
      <p><font face="Courier New, Courier, mono" size="-1">array&lt;<i>type</i>,<i>num</i>&gt;</font></p>
    </td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">default constructor, 
      creates array of <i><font face="Courier New, Courier, mono">num</font></i> 
      element of <i><font face="Courier New, Courier, mono">type</font></i>, see 
      comment below</font></td>
  </tr>
  <tr> 
    <td><font face="Courier New, Courier, mono" size="-1">array&lt;<i>type</i>,<i>num</i>&gt;(<i>a</i>)</font></td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">copy constructor, 
      copies all elements of <i><font face="Courier New, Courier, mono">a</font></i> 
      (<i><font face="Courier New, Courier, mono">a</font></i> must have same 
      <i> <font face="Courier New, Courier, mono">type</font></i><font face="Courier New, Courier, mono"><font face="Arial, Helvetica, sans-serif"> 
      and </font></font><i><font face="Courier New, Courier, mono">num</font></i>)</font></td>
  </tr>
  <tr> 
    <td><font face="Courier New, Courier, mono" size="-1">operator=</font></td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">assignment, assigns 
      all elements</font></td>
  </tr>
  <tr>
    <td><font face="Courier New, Courier, mono" size="-1">assign(<i>val</i>)</font></td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">assigns <i>val</i> 
      to all elements</font></td>
  </tr>
  <tr> 
    <td><font face="Courier New, Courier, mono" size="-1">begin()</font></td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">returns iterator for 
      the first element</font></td>
  </tr>
  <tr> 
    <td><font face="Courier New, Courier, mono" size="-1">end()</font></td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">returns iterator for 
      position after the last element</font></td>
  </tr>
  <tr> 
    <td><font face="Courier New, Courier, mono" size="-1">rbegin()</font></td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">returns reverse iterator 
      for position of first element of reverse iteration</font></td>
  </tr>
  <tr> 
    <td><font face="Courier New, Courier, mono" size="-1">rend()</font></td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">returns reverse iterator 
      for position behind last element of reverse iteration </font></td>
  </tr>
  <tr> 
    <td><font face="Courier New, Courier, mono" size="-1">operator[<i>i</i>]</font></td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">returns element with 
      index <i><font face="Courier New, Courier, mono">i</font></i> (no range 
      checking)</font></td>
  </tr>
  <tr> 
    <td><font face="Courier New, Courier, mono" size="-1">at(<i>i</i>)</font></td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">returns element with 
      index <font face="Courier New, Courier, mono"><i>i</i></font> (throw std::range_error 
      if <i><font face="Courier New, Courier, mono">i</font></i> is not valid)</font></td>
  </tr>
  <tr> 
    <td><font face="Courier New, Courier, mono" size="-1">front()</font></td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">returns first element 
      (caller has to ensure that it exists)</font></td>
  </tr>
  <tr> 
    <td><font face="Courier New, Courier, mono" size="-1">back()</font></td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">returns last element 
      (caller has to ensure that it exists)</font></td>
  </tr>
  <tr> 
    <td><font face="Courier New, Courier, mono" size="-1">data()</font></td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">returns raw element 
      array for read-only element access</font></td>
  </tr>
  <tr> 
    <td><font face="Courier New, Courier, mono" size="-1">size()</font></td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">returns number of 
      elements</font></td>
  </tr>
  <tr> 
    <td><font face="Courier New, Courier, mono" size="-1">empty()</font></td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">returns whether array 
      is empty</font></td>
  </tr>
  <tr> 
    <td><font face="Courier New, Courier, mono" size="-1">max_size()</font></td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">returns maximum possible 
      number of elements (same as size())</font></td>
  </tr>
  <tr> 
    <td><font face="Courier New, Courier, mono" size="-1">swap(a)</font></td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">swap elements with 
      array a</font></td>
  </tr>
  <tr> 
    <td><font face="Courier New, Courier, mono" size="-1">==<font face="Arial, Helvetica, sans-serif">, 
      </font>!=</font></td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">checks for equality</font></td>
  </tr>
  <tr> 
    <td><font face="Courier New, Courier, mono" size="-1">&lt;<font face="Arial, Helvetica, sans-serif">, 
      </font>&lt;=<font face="Arial, Helvetica, sans-serif">, </font>&gt;<font face="Arial, Helvetica, sans-serif">, 
      </font>&gt;=</font></td>
    <td><font face="Arial, Helvetica, sans-serif" size="-1">compares array</font></td>
  </tr>
  <tr> 
    <td><font face="Arial, Helvetica, sans-serif" size="-1"><b>Values:</b></font></td>
    <td>&nbsp;</td>
  </tr>
  <tr> 
    <td><font face="Courier New, Courier, mono" size="-1">static_size</font></td>
    <td><font size="-1" face="Arial, Helvetica, sans-serif">yields size at compile 
      time</font></td>
  </tr>
</table>
<p><font face="Arial, Helvetica, sans-serif" size="-1"><a name="discussion"></a>Class 
  array fulfills most but not all of the requirements of &quot;reversible containers&quot; 
  (see Section 23.1, [lib.container.requirements] of the C++ Standard). The reasons 
  array is not an reversible STL container is because: </font> <font face="Arial, Helvetica, sans-serif" size="-1"><br>
  - No constructors are provided<br>
  - Elements may have an undetermined initial value (see below)<br>
  - swap() has no constant complexity<br>
  - size() is always constant, based on the second template argument of the type<br>
  - The container provides no allocator support</font> 
<p><font face="Arial, Helvetica, sans-serif" size="-1">It doesn't fulfill the 
  requirements of a &quot;sequence&quot; (see Section 23.1.1, [lib.sequence.reqmts] 
  of the C++ Standard), except that</font> <font face="Arial, Helvetica, sans-serif" size="-1"><br>
  - front() and back() are provided<br>
  - operator[] and at() are provided</font>
<p><font face="Arial, Helvetica, sans-serif" size="-1">Regarding the constructors 
  there was an important design tradeoff: We could implement array as an &quot;<b>aggregate</b>&quot; 
  (see Section 8.5.1, [dcl.init.aggr], of the C++ Standard). This would mean:</font></p>
<ul>
  <li><font face="Arial, Helvetica, sans-serif" size="-1">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:</font> 
    <blockquote> 
      <p><font face="Arial, Helvetica, sans-serif" size="-1">boost::array&lt;int,4&gt; 
        a = { { 1, 2, 3 } };</font></p>
    </blockquote>
    <p><font face="Arial, Helvetica, sans-serif" size="-1">Note that if there 
      are fewer elements in the initializer list, then each remaining element 
      gets default-initialized (thus, it has a defined value).</font></p>
  </li>
  <li><font face="Arial, Helvetica, sans-serif" size="-1">However, <b>passing 
    no initializer list means that the elements have an indetermined initial value</b>.</font></li>
  <li><font face="Arial, Helvetica, sans-serif" size="-1">It has no user-declared 
    constructors.</font></li>
  <li><font face="Arial, Helvetica, sans-serif" size="-1">It has no private or 
    protected non-static data members.</font></li>
  <li><font face="Arial, Helvetica, sans-serif" size="-1">It has no base classes.</font></li>
  <li><font face="Arial, Helvetica, sans-serif" size="-1">It has no virtual functions.</font></li>
</ul>
<p><font face="Arial, Helvetica, sans-serif" size="-1">The current implementation 
  uses this approach. However, being able to have indeterminate initial values 
  is a big drawback. So, please give me some feedback, how useful you consider 
  this feature to be. This leads to the list of <b>Open issues:</b></font> 
<ul>
  <li><font face="Arial, Helvetica, sans-serif" size="-1">Do we want initializer 
    list support or would the following be OK?:</font> 
    <blockquote> 
      <p><font face="Courier New, Courier, mono" size="-1">int data[] = { 1, 2, 
        3, 4 }</font></p>
      <p><font face="Courier New, Courier, mono" size="-1">array&lt;int,5&gt; 
        x(data); <font face="Arial, Helvetica, sans-serif">or </font>&nbsp;&nbsp;array&lt;int,data&gt; 
        x;</font></p>
    </blockquote>
  </li>
  <li><font face="Arial, Helvetica, sans-serif" size="-1">Could &quot;<font face="Courier New, Courier, mono">{ 
    </font>...<font face="Courier New, Courier, mono"> }</font>&quot; be used 
    portably instead of &quot;<font face="Courier New, Courier, mono">{ { </font>...<font face="Courier New, Courier, mono"> 
    } }</font>&quot; to initialize values?</font> </li>
  <blockquote> 
    <p><font face="Arial, Helvetica, sans-serif" size="-1">8.5.1 (11) of the Standard 
      seems to allow it; however, gcc 2.95.2 prints a warning message.</font></p>
  </blockquote>
  <li><font face="Arial, Helvetica, sans-serif" size="-1">Any way to have determinate 
    initial values and initializer list support?</font></li>
  <li><font face="Arial, Helvetica, sans-serif" size="-1">Static_casts for reverse 
    iterator stuff?</font></li>
</ul>
<p><font face="Arial, Helvetica, sans-serif" size="-1">I'd appreciate any constructive 
  <a href="mailto:solutions@josuttis.com">feedback</a>. <b>Please note: I don't 
  have time to read all boost mails. Thus, to make sure that feedback arrives 
  me, please send me a copy of each mail regarding this class.</b></font> 
<p><font face="Arial, Helvetica, sans-serif"><a name="code"></a>The code is provided 
  "as is" without expressed or implied warranty.</font> 
<p><font face="Arial, Helvetica, sans-serif"><b>array.hpp</b>, the implementation 
  of <font face="Courier New, Courier, mono">array&lt;&gt;</font><b>:</b> </font> 
<li><font face="Arial, Helvetica, sans-serif">
	<a href="array.hpp.html">as HTML file</a></font></li>
<li><font face="Arial, Helvetica, sans-serif">
	<a href="../../boost/array.hpp">as plain file</a></font></li>
<p> <font face="Arial, Helvetica, sans-serif"><b>array1.cpp</b>, a simple example 
  for using <font face="Courier New, Courier, mono">array&lt;&gt;<font face="Arial, Helvetica, sans-serif">:</font></font></font> 
<li><font face="Arial, Helvetica, sans-serif">
	<a href="array1.cpp.html">as HTML file</a></font> </li>
<li><font face="Arial, Helvetica, sans-serif">
	<a href="array1.cpp">as plain file</a></font></li>
<p> <font face="Arial, Helvetica, sans-serif"><b>array2.cpp</b>, another example 
  for using <font face="Courier New, Courier, mono">array&lt;&gt;<font face="Arial, Helvetica, sans-serif">:</font></font></font> 
<li><font face="Arial, Helvetica, sans-serif">
	<a href="array2.cpp.html">as HTML file</a></font></li>
<li><font face="Arial, Helvetica, sans-serif">
	<a href="array2.cpp">as plain file</a></font></li>
<p> <font face="Arial, Helvetica, sans-serif"><b>array3.cpp</b>, a third example 
  for using <font face="Courier New, Courier, mono">array&lt;&gt;<font face="Arial, Helvetica, sans-serif">:</font></font></font> 
<li><font face="Arial, Helvetica, sans-serif">
	<a href="array3.cpp.html">as HTML file</a></font></li>
<li><font face="Arial, Helvetica, sans-serif">
	<a href="array3.cpp">as plain file</a></font></li>
<p> <font face="Arial, Helvetica, sans-serif"><b>array4.cpp</b>, an example for 
  using <font face="Courier New, Courier, mono">array</font>s of <font face="Courier New, Courier, mono">array</font>s<font face="Courier New, Courier, mono"><font face="Arial, Helvetica, sans-serif">:</font></font></font> 
<li><font face="Arial, Helvetica, sans-serif"> <a href="array4.cpp.html">as HTML 
  file</a></font></li>
<li><font face="Arial, Helvetica, sans-serif"> <a href="array4.cpp">as plain file</a></font></li>
<p><font face="Arial, Helvetica, sans-serif"><b>array5.cpp</b>, an example for 
  testing other operations of <font face="Courier New, Courier, mono">array&lt;&gt;<font face="Arial, Helvetica, sans-serif">:</font></font></font> 
<li><font face="Arial, Helvetica, sans-serif"> <a href="array5.cpp.html">as HTML 
  file</a></font></li>
<li><font face="Arial, Helvetica, sans-serif"> <a href="array5.cpp">as plain file</a></font></li>
<br>
<br>
<font face="Arial, Helvetica, sans-serif">
  To find more details about using ordinary arrays in C++ and the framework of 
  the STL, see e.g.</font> <font face="Arial, Helvetica, sans-serif"><br>
  <i>&nbsp;&nbsp;&nbsp;&nbsp; <a href="http://www.josuttis.com/libbook/">The C++ 
  Standard Library - A Tutorial and Reference</a></i> <br>
  &nbsp;&nbsp;&nbsp;&nbsp; by <a href="http://www.josuttis.com" target="_top">Nicolai 
  M. Josuttis</a></font> <font face="Arial, Helvetica, sans-serif"><br>
  &nbsp;&nbsp;&nbsp;&nbsp; Addison Wesley Longman, 1999</font> <font face="Arial, Helvetica, sans-serif"><br>
  &nbsp;&nbsp;&nbsp;&nbsp; ISBN 0-201-37926-0</font> <font face="Arial, Helvetica, sans-serif"><br>
  </font></li>
<p><font face="Arial, Helvetica, sans-serif"><a href="http://www.josuttis.com/" TARGET="_top">Home 
  Page of Nicolai Josuttis</a></font><font face="Arial, Helvetica, sans-serif"> 
  </font>
<p><font size="-1" face="Arial, Helvetica, sans-serif">[<a href="#intro">intro</a>] 
  [<a href="#interface">interface</a>] [<a href="#discussion">discussion</a>] 
  [<a href="#code">code</a>]</font> 
<p><font face="Arial, Helvetica, sans-serif" size="-1"></font> 
<p><font face="Arial, Helvetica, sans-serif">&nbsp; </font> 
</body>
</html>