Learning Boost 2 Array

xiaoxiao2021-03-06  41

Learning boost 2

Array

Introduction

Boost :: Array is a packaging class for C arrays. Unlike the STL :: Vector, the Array and C arrays are a static size container. Please see one of the following examples:

#include

#include

Using namespace std;

int main ()

{

Boost :: array a = {1, 2, 3, 4, 5};

For (int i = 0; i <5; i)

COUT << a [i] << endl;

Return 0;

}

structure

The prototype of Array is:

Template Class Array;

T represent the type of element, n represents the size of the container. Here, the size N of the container is a parameter of the template, which means that the Array type of different container sizes is different.

Array does not provide a line-type constructor that only provides an overloaded of the equal number operator.

Template Array & Operator = (Const Array );

The constructor, the copy constructor is used by default. Can use the following method to construct Array

Boost :: array a = {1, 2, 3, 4, 5};

Boost :: array b = a; // Using the default copy constructor

Boost :: Array c; // The default constructor, where each element is unknown

Boost :: array d = {1, 2, 3}; // The value of the last two elements is unknown

Boost :: Array E = {1, 2, 3, 4, 5, 6}; // Compiling errors

Boost :: array f = a; // Compiling errors, because Array and Array type

Boost :: array g = a; // correct

access

Like VECTOR, Array supports iterators and Operator [] and AT ();

Operator [] does not provide crossed protection, and AT members will throw an exception for crossed access.

Array supports iterators:

l Iterator

l const_iterator

l Reverse_iterator

l const_reverse_iterator

Related member functions:

l begin ()

l end ()

l rbegin ()

l Rend ()

l Front ()

l back ()

Since these things are the same as the methods used in STL, don't say much.

In addition, Array provides a Data () and c_array () member functions, DATA returns const t *, c_array returns T *, all returns the array of C forms, but pay attention to the member function of Const, C_Array is not.

Since Array is very simple, attach the source code of Array:

/ * The Following Code Declares Class Array,

* An STL Container (as wrapper) for arrays of constant size. *

* See

* Http://www.josuttis.com/cppcode

* for details and the latest version.

* See

* Http://www.boost.org/libs/Array for Documentation.

* for documentation.

*

* (C) Copyright Nicolai M. Josuttis 2001.

* Distributed Under The Boost Software License, Version 1.0. (SeeE

* accompanying file license_1_0.txt or copy at

* http://www.boost.org/license_1_0.txt)

*

* 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.

* 05 AUG 2001 - Minor Update (Nico Josuttis)

* 20 Jan 2001 - Stlport Fix (Beman Dawes)

* 29 Sep 2000 - Initial Revision (Nico Josuttis)

*

* Jan 29, 2004

* /

#ifndef boost_Array_HPP

#define boost_Array_HPP

#include

#include

#include

// Handles Broken Standard Libraries Better Than

#include

#include

// Fixes for Broken Compilers

#include

Namespace boost {

Template

Class array {

PUBLIC:

T elems [n]; // fixed-size array of elements of type t

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 Elems;}

Const_iterator begin () const {return elems;}

Iterator end () {return elems n;}

Const_iterator End () const {return elems n;}

// 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 Reverse_iterator;

Typedef st :: reverse_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 st :: reverse_iterator

Reference, Iterator, Reference>> REVERSE_ITERATOR;

Typedef std :: reverse_iterator

Const_reference, item, reference>> const_reverse_iterator;

#ELSE

// Workaround for Broken Reverse_Istrator Implementations

Typedef std :: Reverse_iterator reverse_iterator;

Typedef std :: reverse_iterator 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)

{

Boost_assert (i

Return Elems [i];

}

Const_reference Operator [] (SIZE_TYPE I) Const

{

Boost_assert (i

Return Elems [I];

}

// at () with Range Check

Reference At (SIZE_TYPE I) {RangeCheck (i); Return Elems [i];

Const_reference At (SIZE_TYPE I) Const {RangeCheck (i); Return Elems [I];} // front () and back ()

Reference Front ()

{

Return Elems [0];

}

Const_reference front () const () const

{

Return Elems [0];

}

Reference back ()

{

Return ELEMS [N-1];

}

Const_reference back () const

{

Return ELEMS [N-1];

}

// size is constant

Static size_type size () {return n;}

Static Bool Empty () {Return False;

Static size_type max_size () {return n;}

ENUM {static_size = n};

// swap (NOTE: LINEAR COMPLEXITY)

Void Swap (Array & Y) {

Std :: swap_ranges (begin (), end (), y.begin ());

}

// Direct Access to Data (Read-ONLY)

Const T * DATA () const {return elems;}

// Use Array As C Array (Direct Read / Write Access to Data)

T * c_Array () {Return Elems;}

// Assignment with Type Conversion

Template

Array & Operator = (Const Array & r Hs) {

Std :: Copy (rhs.begin (), rhs.end (), begin ());

RETURN * THIS;

}

// Assign One Value To All Elements

Void Assign (Const T & Value)

{

Std :: Fill_n (Begin (), Size (), Value;

}

// Check Range (May Be Private Because IT IS Static)

Static void Rangecheck (size_type i) {

IF (i> = size ()) {

Throw std :: Range_ERROR ("Array <>: Index Out of Range);

}

}

}

// commitarisons

Template

Bool Operator == (Const Array & X, Const Array & Y) {

Return std :: equal (x.begin (), x.end (), y.begin ());

}

Template

Bool Operator <(Const Array & X, Const Array & Y) {

Return std :: lexicographical_compare (x.begin (), x.end (), y.begin (), y.end ());

}

Template

Bool Operator! = (const array & x, const array & y) {return! (x == y);

}

Template

Bool Operator> (Const Array & X, Const Array & Y) {

Return Y

}

Template

Bool Operator <= (Const Array & X, Const Array & Y) {

Return! (Y

}

Template

Bool Operator> = (Const Array & X, Const Array & Y) {

Return! (x

}

// Global Swap ()

Template

Inline Void Swap (Array & X, Array & Y) {

x.swap (y);

}

} / * Namespace Boost * /

#ENDIF / * BOOST_ARRAY_HPP * /

转载请注明原文地址:https://www.9cbs.com/read-70814.html

New Post(0)