An exception is thrown if an incorrect index is given (e.g.,a negative index)
at(integer i)
: returns the element at index i without removing itset(integer i, object o)
: replace the element at index i with oinsert(integer i, object o)
: insert a new element o to have index ierase(integer i)
: removes element at index isize()
empty()
index : (f-1+N)%N
How large should the new array be?
Amortized time
#include <iostream>
#include <algorithm>
using std::max;
// Code Fragment 6.2: A vector implementation using an extendable array.
typedef int Elem; // base element type
class ArrayVector
{
public:
ArrayVector(); // constructor
int size() const; // number of elements
bool empty() const; // is vector empty?
Elem &operator[](int i); // element at index
Elem &at(int i) throw(IndexOutOfBounds); // element at index
void erase(int i); // remove element at index
void insert(int i, const Elem &e); // insert element at index
void reserve(int N); // reserve at least N spots
// . . . (housekeeping functions omitted)
private:
int capacity; // current array size
int n; // number of elements in vector
Elem *A; // array storing the elements
};
// Code Fragment 6.3: The simple member functions for class ArrayVector.
ArrayVector::ArrayVector() // constructor
: capacity(0), n(0), A(NULL)
{
}
int ArrayVector::size() const // number of elements
{
return n;
}
bool ArrayVector::empty() const // is vector empty?
{
return size() == 0;
}
Elem &ArrayVector::operator[](int i) // element at index
{
return A[i];
}
// element at index (safe)
Elem &ArrayVector::at(int i) throw(IndexOutOfBounds)
{
if (i < 0 | | i >= n)
throw IndexOutOfBounds("illegal index in function at()");
return A[i];
}
// Code Fragment 6.4: The member function remove for class ArrayVector.
void ArrayVector::erase(int i)
{ // remove element at index
for (int j = i + 1; j < n; j++) // shift elements down
A[j - 1] = A[j];
n--; // one fewer element
}
// Code Fragment 6.5: The member functions reserve and insert for class ArrayVector.
void ArrayVector::reserve(int N)
{ // reserve at least N spots
if (capacity >= N)
return; // already big enough
Elem *B = new Elem[N]; // allocate bigger array
for (int j = 0; j < n; j++) // copy contents to new array
B[j] = A[j];
if (A != NULL)
delete[] A; // discard old array
A = B; // make B the new array
capacity = N; // set new capacity
}
void ArrayVector::insert(int i, const Elem &e)
{
if (n >= capacity) // overflow?
reserve(max(1, 2 * capacity)); // double array size
for (int j = n - 1; j >= i; j--) // shift elements up
A[j + 1] = A[j];
A[i] = e; // put in empty slot
n++; // one more element
}
The Position ADT models the notion of place within a data structure where a single object is stored
begin()
: returns an iterator to the first elementend()
: return an iterator to an imaginary position just after the last element*p
: returns the element referenced by this iterator++p
: advances to the next elementfor (p = C.begin(); p != C.end(); ++p)
loop_body
typedef vector<int>::iterator Iterator;
int sum = 0;
for (Iterator p = V.begin(); p != V.end(); ++p)
sum += *p;
return sum;
size()
, empty()
begin()
: returns an iterator referring to the first element of the list; same as end()
if the list is emptyend()
: returns an iterator referring to an imaginary element just after the last element of the listinsertFront(e)
: inserts a new element e into the list as the first elementinsertBack(e)
: insert a new element e into the list as the last elementeraseFront()
: removes the first element of the listeraseBack()
: removes the last element of the listinsert(p,e)
: inserts a new element e into the list before position p in the listerase(p)
: removes from the list the element at position p; invalidates p as a position벑ν°μ 리μ€νΈλ Sequences ADTμ μΌμ’ μ΄λ€. (λ General νλ€.)
size()
, empty()
at(i)
, set(i,o)
, insert(i,o)
, erase(i)
begin()
, end()
insertFront(o)
, insertBack(o)
eraseFront()
, eraseBack()
insert(p,o)
, erase(p)
atIndex(i)
: returns the position of the element at index iindexOf(p)
: returns the index of the element at position p