United States

Compaq C++

# Compaq C++
Class Library Reference Manual

Previous | Contents | Index

# Chapter 11
vector Package

The vector package provides ways to define vectors or stacks of objects of any type by using the macro expansion capability of the C++ preprocessor.

To declare a generic vector:

1. Include the header `<vector.hxx>` in your program and declare the `vector` class as follows:

       declare(vector, TYPE)

   TYPE may be any valid C++ type name. Make sure you define the `declare` macro in every file that references this new vector data type.
2. Expand the implementation of all function bodies as follows:

       implement(vector, TYPE)

   This `implement` macro must appear once in a program.
3. Declare objects of type `vector` and TYPE and use the index operator to reference these objects. The following is an example of declaration and referencing:

       class MyType {/*...*/};
       declare(vector,MyType)
       implement(vector,MyType)
       vector(MyType) vec1(100), vec2(5);
       MyType x,y;
       //...
       if(vec2[4] == y) vec1[98] = x;

The TYPE parameter must be an identifier. If it is not a class name, a fundamental type, or a type name, create a name for the type using a `typedef` declaration. For example:

    typedef char *PCHAR;
    declare(vector, PCHAR)
    implement(vector, PCHAR)
    implement(vector, PCHAR)

    void f()
    {
        vector(PCHAR) ptrvec(10);
        char *p = "Text";

        ptrvec[0] = p;
        // ...
    }

Thread Safety

The generation of error messages within the vector package is not thread safe; the package relies on static members to handle the current error message and there is no synchonization between threads. If this creates a problem for your application, Digital recommends that you define a single `Mutex` object to synchronize all use of the vector package. For more information on synchronizing access to user-defined objects, see Chapter 6.

# stack(TYPE) class

> Provides a generic (parameterized) data abstraction for a fixed-sized stack of objects of some given type.

## Header

> `#include <vector.hxx>`

> Alternative Header

> `#include <vector.h>`

## Compile-Time Parameter

> TYPE---The type of the objects in the stack. It must be an identifier.

## Declaration

>     class stack(TYPE): private vector(TYPE)
>     {
>     public:
>                          stack(TYPE)(int); // objection size_error
>                          stack(TYPE)(stack(TYPE) &);

>         void             push(TYPE);       // objection overflow_error
>         TYPE             pop();            // objection underflow_error
>         TYPE             &top();           // objection no_top_error
>         int              full();
>         int              empty();
>         int              size();
>         int              size_used();

>         static Objection overflow_error;
>         static Objection underflow_error;
>         static Objection no_top_error;
>     };

## Description

> This class provides a generic (parameterized) data abstraction for a fixed-sized stack of objects of some given type.

> Before a `stack` object can be declared or implemented, the base class, a `vector` object with the same type parameter, must also be declared and implemented. To declare a `stack` object you need to both declare and implement the base vector class and the stack class.

## Exception Handling

> Exceptions are implemented with the Objection package. The initial action function for all objections prints an error message on `cerr` and calls `abort()` .

## Constructors

> ### stack(TYPE)(int size)

> Constructs a `stack` object with room for size elements in the stack. If size is less than or equal to 0, the objection `vector(TYPE)::size_error` is raised.

> ### stack(TYPE)(stack(TYPE) &src)

> Constructs a `stack` object that takes the initial values of the elements from another `stack` object of the same type and size.

## Member Data

> The following objections are raised for the stack errors described.

> > ### static Objection no_top_error

> > Attempted to reference the top of an empty stack.

> > ### static Objection overflow_error

> > Attempted to push too many elements onto the stack.

> > ### static Objection underflow_error

> > Attempted to pop an empty stack.

## Member Functions

> ### int empty()

> Returns `TRUE` if the stack is empty; otherwise, it returns `FALSE` .

> ### int full()

> Returns `TRUE` if the stack is full; otherwise, it returns `FALSE` .

> ### TYPE pop()

> Pops an element off the top of the stack. If the stack underflows, the objection `stack(TYPE)::underflow_error` is raised.

> ### void push(TYPE new_elem)

> Pushes an element onto the stack. If the stack overflows, the objection `stack(TYPE)::overflow_error` is raised.

> ### int size()

> Returns the maximum number of elements in the stack.

> ### int size_used()

> Returns the number of elements currently used in a generic stack.

> ### TYPE &top()

> Returns a reference to the element on the top of the stack. If the stack is empty, the objection `stack(TYPE)::no_top_error` is raised.

## Example

    declare(vector, int)
    implement(vector, int)
    declare(stack, int)
    implement(stack, int)

    void f()
    {
        stack(int) st(20);
        st.push(17);
        // ...
    }

> This example shows the four steps required to declare and implement the base `vector` class and to declare and implement the `stack` class.

## See Also

> Objection Package
> generic Package
> `vector(TYPE)` class

# vector(TYPE) class

> Provides the (parameterized) data abstraction for a fixed-sized vector of objects of some given type.

## Header

> `#include <vector.hxx>`

> Alternative Header

> `#include <vector.h>`

## Compile-Time Parameter

> TYPE---The type of the objects in the vector. It must be an identifier.

## Declaration

>     class vector(TYPE)
>     {
>     public:
>                          // objection size_error
>                          vector(TYPE)(int);
>                          vector(TYPE)(vector(TYPE) &);
>                          ~vector(TYPE)();
>                          // objection copy_size_error
>         vector(TYPE)     &operator=(vector(TYPE) &);
>         TYPE             &elem(int);
>                          // objection index_error
>         TYPE             &operator[](int);
>         int              size();
>         void             set_size(int);

>         static Objection size_error;
>         static Objection copy_size_error;
>         static Objection index_error;
>     };

## Description

> This class provides the (parameterized) data abstraction for a fixed-sized vector of objects of some given type.

## Exception Handling

> Exceptions are implemented with the Objection package. The initial action function for all objections prints an error message on `cerr` and calls `abort()` .

## Constructors and Destructors

> ### vector(TYPE)(int new_size)

> Constructs a `vector` object with the integer argument representing the number of elements in the vector. If the number of elements is less than or equal to 0, the objection `vector(TYPE)::size_error` is raised.

> ### vector(TYPE)(vector(TYPE) &src)

> Constructs a `vector` object that takes initial values of the elements from another `vector` object of the same type and size.

> ### ~vector(TYPE)()

> Deletes a `vector` object.

## Member Data

> The following objections are raised for the vector errors described.

> > ### static Objection copy_size_error

> > Attempted to assign a vector to another vector that has a different number of elements.

> > ### static Objection index_error

> > Attempted to reference a vector element with a subscript out of range.

> > ### static Objection size_error

> > Attempted to create a vector with less than one element in it.

## Overloaded Operators

> ### vector(TYPE) &operator = (vector(TYPE) &src)

> Assigns a vector to another vector. If the sizes of the vectors are different, the objection `vector(TYPE)::copy_size_error` is raised.

> ### TYPE &operator [] (int i)

> Returns a reference to the ith element in the vector. The value of i has a range from 0 to `size()` --1. If the subscript is out of bounds, the objection `vector(TYPE)::index_error` is raised.

## Other Member Functions

> ### TYPE &elem(int i)

> Behaves like `operator []` but without bounds checking.

> ### void set_size(int new_size)

> Changes the size of the vector.

> ### int size()

> Returns the number of elements in the vector.

## See Also

> Objection Package
> generic Package

Index | Contents

privacy and legal statement