diff --git a/array.hpp.html b/array.hpp.html deleted file mode 100644 index 964837b..0000000 --- a/array.hpp.html +++ /dev/null @@ -1,175 +0,0 @@ - -
-- - array.hpp - - |
Class - array, an STL Container (as - Wrapper) for Arrays of Constant Size | -
[intro] - [interface] [discussion] - [code] -
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). -
As replacement for ordinary - arrays, the STL provides class vector<>. - However, vector<> 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. -
In his book, Generic - Programming and the STL, Matthew H. Austern introduces a useful wrapper - class for ordinary arrays with static size, called block. - It is safer and has no worse performance than ordinary arrays. In The C++ - Programming Language, 3rd edition, Bjarne Stroustrup introduces a similar - class, called c_array, - which I (Nicolai Josuttis) present slightly - modified in my book The C++ Standard Library - A Tutorial and Reference, - called carray. This is - the essence of these approaches spiced with many feedback from boost. -
After considering different - names, we decided to name this class simply array. -
The - class provides the following interface: -
Types: | -- |
value_type | -type of the elements | -
iterator | -type of the iterator - (random-access iterator) | -
const_iterator | -type of iterator that - considers elements as being constant | -
reference | -type of element reference | -
const_reference | -type of element reference - that considers elements as being constant | -
size_type | -type for signed size - values | -
difference_type | -type for unsigned - difference values | -
Operations: | -- |
- array<type,num> - |
- default constructor, - creates array of num - element of type, see - comment below | -
array<type,num>(a) | -copy constructor, - copies all elements of a - (a must have same - type - and num) | -
operator= | -assignment, assigns - all elements | -
assign(val) | -assigns val - to all elements | -
begin() | -returns iterator for - the first element | -
end() | -returns iterator for - position after the last element | -
rbegin() | -returns reverse iterator - for position of first element of reverse iteration | -
rend() | -returns reverse iterator - for position behind last element of reverse iteration | -
operator[i] | -returns element with - index i (no range - checking) | -
at(i) | -returns element with - index i (throw std::range_error - if i is not valid) | -
front() | -returns first element - (caller has to ensure that it exists) | -
back() | -returns last element - (caller has to ensure that it exists) | -
data() | -returns raw element - array for read-only element access | -
size() | -returns number of - elements | -
empty() | -returns whether array - is empty | -
max_size() | -returns maximum possible - number of elements (same as size()) | -
swap(a) | -swap elements with - array a | -
==, - != | -checks for equality | -
<, - <=, >, - >= | -compares array | -
Values: | -- |
static_size | -yields size at compile - time | -
Class
- array 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:
- - No constructors are provided
- - Elements may have an undetermined initial value (see below)
- - swap() has no constant complexity
- - size() is always constant, based on the second template argument of the type
- - The container provides no allocator support
-
It doesn't fulfill the
- requirements of a "sequence" (see Section 23.1.1, [lib.sequence.reqmts]
- of the C++ Standard), except that
- - front() and back() are provided
- - operator[] and at() are provided
-
Regarding the constructors - there was an important design tradeoff: We could implement array as an "aggregate" - (see Section 8.5.1, [dcl.init.aggr], of the C++ Standard). This would mean:
---boost::array<int,4> - a = { { 1, 2, 3 } };
-
Note that if there - are fewer elements in the initializer list, then each remaining element - gets default-initialized (thus, it has a defined value).
-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 Open issues: -
--int data[] = { 1, 2, - 3, 4 }
-array<int,5> - x(data); or array<int,data> - x;
-
--8.5.1 (11) of the Standard - seems to allow it; however, gcc 2.95.2 prints a warning message.
-
I'd appreciate any constructive - feedback. 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. -
The code is provided - "as is" without expressed or implied warranty. -
array.hpp, the implementation - of array<>: -
array1.cpp, a simple example - for using array<>: -
array2.cpp, another example - for using array<>: -
array3.cpp, a third example - for using array<>: -
array4.cpp, an example for - using arrays of arrays: -
array5.cpp, an example for - testing other operations of array<>: -
Home - Page of Nicolai Josuttis - -
[intro] - [interface] [discussion] - [code] -
-
-
-
diff --git a/array1.cpp b/array1.cpp
deleted file mode 100644
index b5f45c1..0000000
--- a/array1.cpp
+++ /dev/null
@@ -1,59 +0,0 @@
-/* simple example for using class array<>
- *
- * (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.
- * This software is provided "as is" without express or implied
- * warranty, and with no claim as to its suitability for any purpose.
- *
- * Changelog:
- * 20 Jan 2001 - Removed boolalpha use since stock GCC doesn't support it
- * (David Abrahams)
- */
-
-#include
-
-
- array1.cpp
-
-
-
-
-
-
-/* simple example for using class array<>
- */
-#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: " << std::boolalpha << a.empty() << 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
-}
-
-
-
-
-