|
Prev | Next |
# include <cppad/vector.hpp>
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:
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.
x
is a
CppAD::vector<Scalar>
,
and
cap
is a size_t object,
cap = x.capacity()
set
cap
to the number of
Scalar
objects that
could fit in the memory currently allocated for
x
.
Note that
x.size() <= x.capacity()
x
and
y
are
CppAD::vector<Scalar>
objects,
y = x
has all the properties listed for a
simple vector assignment
plus the following:
CppAD::vector template class will check that
the size of
x
is either zero or the size of
y
before doing the assignment.
If this is not the case, CppAD::vector will use
ErrorHandler
to generate an appropriate error report.
Allowing for assignment to a vector with size zero makes the following
code work:
CppAD::vector<Scalar> y;
y = x;
y
is returned.
An example use of this reference is in multiple assignments of the form
z = y = x
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.
x
is a
CppAD::vector<Scalar>
object
with size equal to
n
and
s
has type
Scalar
,
x.push_back(s)
extends the vector
x
so that its new size is
n
plus one
and
x[n]
is equal to
s
(equal in the sense of the
Scalar
assignment operator).
x
is a
CppAD::vector<Scalar>
object
with size equal to
n
and
v
is a simple vector
with elements of type
Scalar
and size
m
,
x.push_vector(v)
extends the vector
x
so that its new size is
n+m
and
x[n + i]
is equal to
v[i]
for
i = 1 , ... , m-1
(equal in the sense of the
Scalar
assignment operator).
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
.
x.resize(n)
set the size of
x
equal to
n
.
If
n <= x.capacity()
,
no memory is freed or allocated and the capacity of
x
does not change.
<cppad/vector.hpp> also defines the class
CppAD::vectorBool.
This has the same specifications as CppAD::vector<bool>
with the following exceptions
vectorBool conserves on memory
(on the other hand, CppAD::vector<bool> is expected to be faster
than vectorBool).
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.
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.
thread_alloc)
to the current thread.
CppAD::vector<double> x(3);
size_t i;
for(i = 0; i < 3; i++)
x[i] = 4. - i;
std::cout << "x = " << x << std::endl;