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

©Copyright 1996 Rogue Wave Software

RWTValSortedVector<T>

Alternate template: Standard C++ Library not required

Synopsis

#include <rw/tvsrtvec.h>
RWTValSortedVector<T> sortvec;

Please Note!


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


Description

RWTValSortedVector<T> is an ordered 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 RWTValSortedVector<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 copy of the inserted item into the collection according to an ordering determined by the less-than (<) operator.

The class T must have:

Note that a sorted vector has a length (the number of items returned by length() or entries()) and a capacity. Necessarily, the capacity is always greater than or equal to the length. Although elements beyond the collection's length are not used, nevertheless, in a value-based collection, they are occupied. If each instance of class T requires considerable resources, then you should ensure that the collection's capacity is not much greater than its length, otherwise unnecessary resources will be tied up.

Although it is possible to alter objects that are contained in aTValSortedVector<T>, it is dangerous since the changes may affect the way that operator<() and operator==() behave, causing the RWTValSortedVector<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/tvsrtvec.h>
#include <rw/rwdate.h>
#include <rw/rstream.h>

{
  RWTValSortedVector<RWDate> vec;

  vec.insert(RWDate(10, "Aug", 1999));
  vec.insert(RWDate(9, "Aug", 1999));
  vec.insert(RWDate(1, "Sept", 1999));
  vec.insert(RWDate(14, "May", 1999));
  vec.insert(RWDate(1, "Sept", 1999));     // Add a duplicate
  vec.insert(RWDate(2, "June", 1999));

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

Program output

May 14, 1999
June 2, 1999
August 9, 1999
August 10, 1999
September 1, 1999
September 1, 1999

Public Constructor

RWTValSortedVector(size_t capac = RWDEFAULT_CAPACITY);

Public Operators

T&
operator()(size_t i);
const T&
operator()(size_t i) const;
T&
operator[](size_t i);
const T&
operator[](size_t i) const;

Public Member Functions

T&
at(size_t i);
const T&
at(size_t i) const;
void
clear();
RWBoolean
contains(const T& a) const;
const T*
data() const;
size_t
entries() const;
RWBoolean
find(const T& target, T& ret) const;
const T&
first() const;
size_t
index(const T& a) const;
void
insert(const T& a);
RWBoolean
isEmpty() const;
const T&
last() const;
size_t
length() const;
size_t
occurrencesOf(const T& a) const;
RWBoolean
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 RWTValSortedVector<T>& coll);
RWFile&
operator<<(RWFile& strm, const RWTValSortedVector<T>& coll);
RWvistream&
operator>>(RWvistream& strm, RWTValSortedVector<T>& coll);
RWFile&
operator>>(RWFile& strm, RWTValSortedVector<T>& coll);
RWvistream&
operator>>(RWvistream& strm, RWTValSortedVector<T>*& p);
RWFile&
operator>>(RWFile& strm, RWTValSortedVector<T>*& p);