Boost source code brief analysis series --ARAY (1)
Array is a class written by Nicolai M.JOSuttis for the Boost extension. She adds some of the features of the container for static arrays, such as increasing iterators, similar to the elementality of the vectors, and more.
The following is a brief introduction part of Boost Since the Boost documentation.
C Standard Template (STL) As part of the C standard library, she provides a wide variety of container classes, however, for ordinary array classes, STL does not provide interfaces with their container class, such as Iterator interfaces. Of course, STL uses std :: vector as a substitute for ordinary arrays. The STL's VECTOR class is different from a normal array. She is a dynamic array, which is to dynamically change the number of stored elements. Functionally, Vector far beyond the features provided by a normal array. However, it is due to the complicated vector function, so there is a gap between the performance of the internal data type in terms of performance.
Similar to the Boost :: Array class, there are several other, for example, in the "Generic Programming and the STL" of Matthew H, she introduces a packaging class Block, she is more secure than ordinary arrays (in operation, For example, overflowing is overflowing, and the performance is not lost. A class C_Array is introduced in Bjarne Stroustrup's "The C Programming Language, 3rd Edition". And the array, which is introduced in this article, is called Carray in Nicolai Josuttis's book "THE C Standard Library-a Tutorial and Reference", and then several times, and finally named Boost :: array.
Boost :: Array achieves most but not all STL container interfaces, so boost :: array cannot be a real STL container class.
The first is, Boost :: Array does not provide a user-defined constructor.
Second, the elements of the object will have an uncertain initial value, which is also caused by the constructor.
Once again, the complexity of the SWAP () function is constant.
Then, the data capacity of its object cannot be changed, and she is determined by the second template parameter that is created.
Finally, this class does not provide support for the dispenser.
This is the difference between her with a STL container class.
Let's take a look at a simple routine using the Boost :: Array class.
//example 1
#include
#include
#include
#include
Int main (void)
{
Boost :: array
{"Spring", "Summer", "Autumn", "Winter"}
}
Boost :: array
Boost :: Array
For (POS = seasons.begin (); pOS! = seasons.end (); POS)
{
Std :: cout << * pOS << '/ n';}
For (reverse_pos = seasons.rbegin (); reverse_pos { Std :: cout << * reverse_pos << '/ n'; } System ("pause"); Return 0; } This routine is debugged on DEV-C 4.9.8.0, but debugging on VC6.0 cannot pass. Here we see Boost :: Array Create an object to provide two template parameters, the previous one is the element type, the latter parameter defines the number of elements that this object can be placed. On the other hand, we use an iterator like the STL container class and travers it through an iterator object and accesses all the elements in the Array object, and print them. But the features provided by Array are more than this. Boost :: Array class defines in Array.hpp. Below is her definition source code: 1. Contains header files #ifndef boost_Array_HPP #define boost_Array_HPP #include #include #include #include #include // ... #ENDIF 2. Define the type name of a set of standards in the ARRAY class Template Class array { PUBLIC: T elems [n]; PUBLIC: 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; !! #If defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && defined (BOOST_MSVC_STD_ITERATOR) && defined (BOOST_NO_STD_ITERATOR_TRAITS) typedef std :: reverse_iterator Typedef st :: reverse_iterator #ELIF Defined (_MSC_VER) && (_MSC_VER == 1300) && Defined (boost_dinkumware_stdlib) && (boost_dinkumware_stdlib == 310) Typedef st :: reverse_iterator Typedef st :: reverse_iterator #ELSE Typedef std :: Reverse_iterator Typedef std :: reverse_iterator #ENDIF // ... } Value_type Element type in the object. Iterator Iterator type. Reference Element reference types in the object. SIZE_TYPE Represents the number of elements in the object. Difference_type The result of the difference between the two iterator of the container. Reverse-Iterator Reverse iterator type. Just like the container in STL, you must define a similar member type when defined, so that the user can write the code using the container without having to know the actual type involved. This is a method of designing a class or library, which is usually used, which increases the transplantability and readability and is a good programming style. 3. Iterator member function. Defining the membership type can then define a member function. // ... T elems [n]; // Define an array, type T, the number of elements is N. // ... Iterator Begin () {Return Elems;} Const_iterator begin () const {return elems;} Iterator end () {return elems n;} Const_iterator End () const {return elems n;} // ... 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 ()); } // ... Begin () Returns the first address of the array, that is, the first element address. End () Returns the nth after the first element of the array, that is, the last element. Rbegin () End () Rend () BEGIN () The use of this container is usually used to use this set of functions, especially when traversing elements, is mainly achieved by them, in Example 1, to print out all of the elements, becomes () and end (). RBEGIN () and rend () are to be able to reverse iterations through the individual elements of the container. The reverse iterator is also a full-deckless conventional iterator, and knowledge about iterators can refer to the "THE C Programming Language, 3rd Edition" section of Bjarne Stroustroup. Rbegin () Returns the last element position of the element sequence in the container, rend () returns a position before the first element of the element sequence. 4. Element Access In addition to accessing elements with an iterator, users also need a set of functions to randomly access any elements in the container in any order in any order, which is also a normal array to access the elements by subscript. When we use a normal array for random access elements, the array does not provide upper overflow detection, providing an access method provides an accessed anomaly detection and throw out an abnormality function AT (). // ... Reference Operator [] (size_type i) {return elems [i]; Const_reference Operator [] (size_type i) const {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]; Reference front () {return elems [0]; Const_reference front () const {return elems [0]; Reference back () {Return Elems [N-1]; Const_reference back () const {return elems [n-1]; // ... Operator [] The subscript operator is overloaded, and the elements in the container are randomly accessed with the subscript. AT () Use the input parameters to randomly access the elements in the container. Front () Access the first element in the element sequence. BACK () Accessing element sequences tail elements. These four functions returned to the elements of the elements, the two functions, Operator [], and AT () are basically consistent to determine the random access function based on the output parameters when the call function is called. The difference is that the former will not be immersed when the access location is located outside the element sequence position, and the latter is to THROW to access overflow exceptions. For Front () and back (), the most commonly used functions when an array is operated. When using the Boost :: Array () constructed queue or stack, these two functions play a big role. For this set of functions, see Example 2. Since this article is a bit long, I am divided into two, in the following, in the next sheet. Sixsavage (Savage) in March 22, 2004 Sixsavage@yahoo.com