Prev Next

The CppAD::vector Template Class

Syntax
# include <cppad/vector.hpp>


Description
The include file cppad/vector.hpp defines the vector template class CppAD::vector. This is a SimpleVector template class and in addition it has the features listed below:

Include
The file cppad/vector.hpp is included by cppad/cppad.hpp but it can also be included separately with out the rest of the CppAD include files.

Assignment
If x and y are CppAD::vector<Scalar> objects,
     
y = x
has all the properties listed for a simple vector assignment plus the following:

The CppAD::vector template class will check that the size of x is equal to the size of y before doing the assignment. If the sizes are not equal, CppAD::vector will use ErrorHandler to generate an appropriate error report.

A reference to the vector y is returned. An example use of this reference is in multiple assignments of the form
     
z = y = x

Element Access
If x is a CppAD::vector<Scalar> object and i has type size_t,
     
x[i]
has all the properties listed for a simple vector element access plus the following:

The object x[i] has type Scalar (is not possibly a different type that can be converted to Scalar).

If i is not less than the size of the x, CppAD::vector will use ErrorHandler to generate an appropriate error report.

push_back
If x is a CppAD::vector<Scalar> object with size equal to n and t has type Scalar,
     
x.push_back(t)
extends the vector x so that its new size is n plus one and x[n] is equal to t (in the sense of the Scalar assignment operator).

Output
If x is a CppAD::vector<Scalar> object and os is an std::ostream, and the operation
     
os << x
will output the vector x to the standard output stream os. The elements of x are enclosed at the beginning by a { character, they are separated by , characters, and they are enclosed at the end by } character. It is assumed by this operation that if e is an object with type Scalar,
     
os << e
will output the value e to the standard output stream os.

resize
If the resize member function is called with argument value zero, all memory allocated for the vector will be freed. The can be useful when using very large vectors and when checking for memory leaks (and there are global vectors).

vectorBool
The file <cppad/vector.hpp> also defines the class CppAD::vectorBool. This has the same specifications as CppAD::vector<bool> with the following exceptions
  1. The class vectorBool conserves on memory (on the other hand, CppAD::vector<bool> is expected to be faster than vectorBool).
  2. The CppAD::vectorBool output operator prints each boolean value as a 0 for false, a 1 for true, and does not print any other output; i.e., the vector is written a long sequence of zeros and ones with no surrounding {, } and with no separating commas or spaces.
  3. If x has type vectorBool and i has type size_t, the element access value x[i] has an unspecified type (referred to here as elementType) that can be implicitly converted to bool. The return value of the assignment operator
         
    x[i] = y
    also has type elementType. Thus, if z has type bool, the syntax
         
    z = x[i] = y
    is valid.


Example
The files CppAD_vector.cpp and vectorBool.cpp each contain an example and test of this template class. They return true if they succeed and false otherwise.

Exercise
Create and run a program that contains the following code:
     CppAD::vector<double> x(3);
     size_t i;
     for(i = 0; i < 3; i++)
          x[i] = 4. - i;
     std::cout << "x = " << x << std::endl;

Input File: cppad/vector.hpp