Boost source code brief analysis series --ARAY (2)
1. Assignment function, etc.
// ...
Static size_type size () {return n;}
Static Bool Empty () {Return False;
Static size_type max_size () {return n;}
ENUM {static_size = n};
Void Swap (Array
Std :: swap_ranges (begin (), end (), y.begin ());
}
Const T * DATA () const {return elems;}
Template
Array
Std :: Copy (rhs.begin (), rhs.end (), begin ());
Return * this;
}
Void Assign (Const T & Value)
{
Std :: Fill_n (Begin (), Size (), Value;
}
Private:
Static void Rangecheck (size_type i) {
IF (i> = size ()) {throw std :: range_error ("array");}
}
// ...
Size ()
The number of Array container energy capacity elements.
EMPTY ()
Is the container empty? ? ?
Max_size ()
The container can accommodate the number of elements.
SWAP ()
Exchange the corresponding elements in the two container element sequences.
Data ()
Returns the argument address of the container to store the elements.
Operator = ()
Heavy load assignment function.
Assign ()
Use a value to replace the value of an element in a sequence of elements.
RangeCheck ()
Check if the container is accessed.
In this group of functions, there are several questions I don't know what the code writer is thinking.
The first is that since Boost :: Array is replaced by a static array, then for the number of elements that define the elements that can be stored, the number of elements that can be stored will be fixed, and they cannot be modified, then there is no need to provide two Member function:
Size () and max_size (). What's more, these two functions are except that the name is different, and other operations are the same. Perhaps Josuttis is given the extension of this class to reserve space.
Second, the EMPTY () function is also fascinating, it seems to be a method of judging whether the container is empty, but take a closer look, but it returns a FALSE.
Then, DATA (), actually returned directly to the address of the first element in the container. I think the user directly accessed the element directly is not Josuttis.
Finally, the RangeCheck () function, checks if the access location overflows, only pays attention to overflow, not notice possible to overflow.
I am not accuseing Josuttis has no rigorous application, these seemingly vulnerable places, is the author to leave in order to expand the expansion.
The remaining three functions are assignment functions:
SWAP (), exchanging all elements in the A container with the elements of the corresponding position in the B vessel. A is called a SWAP () function. Use the STL bid bid to SWAP_RANGES ().
Operator = (), assign a value to the A container with a B container, and the A container calls the Operator = () function.
Assign (), with a specified value for replacing the value of the elements in a sequence in the container. The standard algorithm Fill_N () is used.
This group of functions are used, see Example 2.
2. Compare function
// ...
Template
Bool Operator == (Const Array
Return std :: equal (x.begin (), x.end (), y.begin ());
}
Template
Bool Operator <(Const Array
Return std :: lexicographical_compare (x.begin (), x.end (), y.begin (), y.end ());
}
Template
Bool Operator! = (Const Array
Return! (x == Y);
}
Template
Bool Operator> (Const Array
Return Y } Template Bool Operator <= (Const Array Return! (Y } Template Bool Operator> = (Const Array Return! (x } Template Inline Void Swap (Array x.swap (y); } // ... Operator == () Compares whether the elements sequences in the two containers are equal. Operator <() Compare the element sequence in two containers to sort in the dictionary. Operator! = () It is determined whether or not the elements sequences in the two containers do not wait. Operator> () Compare the element sequence in two containers to sort in the dictionary which is large. Operator <= () Compare two containers, whether the former is less than or equal to the latter. Operator> = () Compare two containers, whether the former is greater than equal to the latter. SWAP () Provides SWAP functionality in the form of static. This set of comparison functions is made based on the standard algorithm of the C standard library. Operator == () In order to compare whether the corresponding elements in the two sequences are equal, equally () in the standard algorithm is used. If the two sequences correspond to the same elements. Operator <() In the case of comparing two sequences, who is in the word order, who is small, the former is very true. Used lexicographpic_compare (). Other functions, it is based on == and The above is, Boost :: Array provides all the resources and its source code. From these source code we can see, technical relying on C standard library algorithm. Provides most STL container interfaces to users, allowing users to access, compare elements in the container. Over-range access can also be avoided via an At () access element. In terms of efficiency, almost all of the implementations of MEMBER FUNCTION are very simple, the compiler looks all the inline functions, and the efficiency does not decline than the ordinary array. So always say, this boost :: array is an excellent static array container class. It can be used as an alternative to a normal array. Below two examples, the use of the Array class is implemented. // Example 2 #include #include #include #include Int main (void) { Boost :: array {"Spring", "Summer", "Autumn", "Winter"} } Std :: Cout << Seasons [2] << '/ n'; Std :: cout << Seasons.at (2) << '/ n'; Std :: cout << seasons.front () << '/ n'; Std :: cout << seasons.back () << '/ n'; Std :: cout << seasons.size () << '/ n'; Std :: cout << seasons.max_size () << '/ n'; Boost :: array {"Spring", "Summer", "Autumn"}}; IF (Seasons == _Seasons) { Std :: cout << "Equality." << '/ n'; } Else IF (_Seasons { Std :: cout << "_ seasons is more smaller." << '/ n'; } Else { Std :: cout << "Seasons is more smaller." << '/ n'; } System ("pause"); Return 0; } As with the same example 1, Example 2 also compiled on DEV-C , and VC told me that there was a syntax error, I halo. references: Boost :: array English document; Bjarne Stroustrup "The C Programming Language, 3rd Edition". Sixsavage (Savage) in March 22, 2004 Sixsavage@yahoo.com