19.8.2.  Vectors and operator[]

[ fromfile: useroperators.xml id: None ]

Many lists and array-like classes offer an interface consistent with arrays, but with additional functionality. Here is an example of a container class that has

Example 19.7. src/operators/vect1/vect1.h

[ . . . . ]
class Vect {
public:
    explicit Vect(int n = 10);
    ~Vect() {
        delete []m_P;
    }
    int& operator[](int i) {   1
        assert (i >= 0 && i < m_Size);
        return m_P[i];
    }
    int  ub() const {
        return (m_Size - 1);
    }   2
private:
    int*  m_P;
    int   m_Size;
};

Vect::Vect(int n) : m_Size(n) {
    assert(n > 0);
    m_P = new int[m_Size];
}
[ . . . . ]

1

access m_P[i]

2

upper bound

In Example 19.8, we define an array of Vect objects. This gives us something akin to a matrix-like structure, where there is one fixed dimension and one that is possibly variable, or sparse, depending on the implementation details of Vect.

Example 19.8. src/operators/vect1/vect1test.cpp

#include "vect1.h"

int main() {
    Vect a(60), b[20];

    b[1][5] = 7;
    cout << " 1 element 5 = "<< b[1][5] << endl;
    for (int i = 0; i <= a.ub(); ++i)
        a[i] = 2 * i + 1;
    for (int i = a.ub(); i >= 0; --i)
        cout << ((a[i] < 100) ? " " : "" )
        << ((a[i] < 10) ? " " : "" )
        << a[i]
        << ((i % 10) ? "  " : "\n");
    cout << endl;
    cout << "Now try to access an out-of-range index"
    << endl;
    cout << a[62] << endl;
}


Output:

OOP> g++ vect1test.cc OOP> a.out 1 element 5 = 7 119 117 115 113 111 109 107 105 103 101 99 97 95 93 91 89 87 85 83 81 79 77 75 73 71 69 67 65 63 61 59 57 55 53 51 49 47 45 43 41 39 37 35 33 31 29 27 25 23 21 19 17 15 13 11 9 7 5 3 1 Now try to access an out-of-range index a.out: vect1.h:12: int &Vect::operator[] (int): Assertion `i >= 0 && i < m_Size' failed. Aborted OOP>