Member Functions | |
data() length() operator()() operator=() operator[]() reshape() |
resize() |
#include <rw/tpvector.h> RWTPtrVector<T> vec;
Class RWTPtrVector<T> is a simple parameterized vector of pointers to objects of type T. It is most useful when you know precisely how many pointers must be held in the collection. If the intention is to "insert" an unknown number of objects into a collection, then class RWTPtrOrderedVector<T> may be a better choice.
The class T can be of any type.
Isomorphic
#include <rw/tpvector.h> #include <rw/rwdate.h> #include <rw/rstream.h> main() { RWTPtrVector<RWDate> week(7); RWDate begin; // Today's date for (int i=0; i<7; i++) week[i] = new RWDate(begin++); for (i=0; i<7; i++) { cout << *week[i] << endl; delete week[i]; } return 0; }
Program output:
March 16, 1996 March 17, 1996 March 18, 1996 March 19, 1996 March 20, 1996 March 21, 1996 March 22, 1996
RWTPtrVector<T>();
Constructs an empty vector of length zero.
RWTPtrVector<T>(size_t n);
Constructs a vector of length n. The initial values of the elements are undefined. Hence, they can (and probably will) be garbage.
RWTPtrVector<T>(size_t n, T* ival);
Constructs a vector of length n, with each element pointing to the item *ival.
RWTPtrVector<T>(const RWTPtrVector& v);
Constructs self as a shallow copy of v. After construction, pointers held by the two vectors point to the same items.
RWTPtrVector<T>& operator=(const RWTPtrVector<T>& v);
Sets self to a shallow copy of v. Afterwards, the two vectors will have the same length and pointersheld by the two vectors will point to the same items.
RWTPtrVector<T>& operator=(T* p);
Sets all elements in self to point to the item *p.
T*& operator()(size_t i); T* operator()(size_t i) const;
Returns the ith value in the vector. The first variant can be used as an l-value, the second cannot. The index i must be between zero and the length of the vector, less one. No bounds checking is performed.
T*& operator[](size_t i); T* operator[](size_t i) const;
Returns the ith value in the vector. The first variant can be used as an lvalue, the second cannot. The index i must be between zero and the length of the vector, less one; or an exception of type TOOL_INDEX will be thrown.
T* const * data() const;
Returns a pointer to the raw data of the vector. Should be used with care.
size_t length() const;
Returns the length of the vector.
void reshape(size_t N);
Changes the length of the vector to N. If this results in the vector being lengthened, then the initial value of the additional elements is undefined.
void resize(size_t N);
Changes the length of the vector to N. If this results in the vector being lengthened, then the initial value of the additional elements is set to nil.