Click on the banner to return to the Class Reference home page.
Return to the Appendix home page.

©Copyright 1996 Rogue Wave Software

RWTPtrSortedVector<T>

Alternate template: Standard C++ Library not required

Synopsis

#include <rw/tpsrtvec.h>
RWTPtrSortedVector<T> sortvec;

Please Note!


If you do not have the Standard C++ Library, use the interface described here. Otherwise, use the interface to RWTPtrSortedVector described in the Class Reference.


Description

RWTPtrSortedVector<T> is a pointer-based sorted collection. That is, the items in the collection have a meaningful ordered relationship with respect to each other and can be accessed by an index number. In the case of RWTPtrSortedVector<T>, objects are inserted such that objects "less than" themselves are before the object, objects "greater than" themselves after the object. An insertion sort is used. Duplicates are allowed.

Stores a pointer to the inserted item into the collection according to an ordering determined by the less-than (<) operator.

The class T must have:

Although it is possible to alter objects that are referenced by pointers within a RWTPtrSortedVector<T>, it is dangerous since the changes may affect the way that operator<() and operator==() behave, causing the RWTPtrSortedVector<T> to become unsorted.

Persistence

Isomorphic

Example

This example inserts a set of dates into a sorted vector in no particular order, then prints them out in order.

#include <rw/tpsrtvec.h>
#include <rw/rwdate.h>
#include <rw/rstream.h>

main()  {
  RWTPtrSortedVector<RWDate> vec;

  vec.insert(new RWDate(10, "Aug", 1991));
  vec.insert(new RWDate(9, "Aug", 1991));
  vec.insert(new RWDate(1, "Sep", 1991));
  vec.insert(new RWDate(14, "May", 1990));
  vec.insert(new RWDate(1, "Sep", 1991));  // Add a duplicate
  vec.insert(new RWDate(2, "June", 1991));

  for (int i=0; i<vec.length(); i++)
    cout << *vec[i] << endl;

  vec.clearAndDestroy();

  return 0;
}
 

Program output

May 14, 1990
June 2, 1991
August 9, 1991
August 10, 1991
September 1, 1991
September 1, 1991

Public Constructor

RWTPtrSortedVector(size_t capac = RWDEFAULT_CAPACITY);
RWTPtrSortedVector<T>(const RWTPtrSortedVector<T>& c);

Public Operators

RWTPtrSortedVector<T>&
operator=(const RWTPtrSortedVector& c);
T*&
operator()(size_t i);
T* const&
operator()(size_t i) const;
T*&
operator[](size_t i);
T* const&
operator[](size_t i) const;

Public Member Functions

T*&
at(size_t i);
T* const&
at(size_t i) const;
void
clear();
void
clearAndDestroy();
RWBoolean
contains(const T* a) const;
T* const *
data() const;
size_t
entries() const;
T*
find(const T* target) const;
T* const&
first() const;
size_t
index(const T* a) const;
void
insert(T* a);
RWBoolean
isEmpty() const;
T* const&
last() const;
size_t
length() const;
size_t
occurrencesOf(const T* a) const;
T*
remove(const T* a);
size_t
removeAll(const T* a);
T*
removeAt(size_t i);
T*
removeFirst();
T*
removeLast();
void
resize(size_t N);

Related Global Operators

RWvostream&
operator<<(RWvostream& strm, 
       const RWTPtrSortedVector<T>& coll);
RWFile&
operator<<(RWFile& strm, const RWTPtrSortedVector<T>& coll);
RWvistream&
operator>>(RWvistream& strm, RWTPtrSortedVector<T>& coll);
RWFile&
operator>>(RWFile& strm, RWTPtrSortedVector<T>& coll);
RWvistream&
operator>>(RWvistream& strm, RWTPtrSortedVector<T>*& p);
RWFile&
operator>>(RWFile& strm, RWTPtrSortedVector<T>*& p);