Tools.h++ Manual
23-28 104011 Tandem Computers Incorporated
23
Example Here's an example of a sorted vector of ints:
Public constructors
RWGSortedVector(
val
)( int (*f)(const
val
*, const
val
*) );
Construct a sorted vector of elements of type val, using the comparison
function pointed to by
f
. The initial capacity of the vector will be set by the
value
RWDEFAULT_CAPACITY
. The capacity will automatically be increased
should too many items be inserted.
RWGSortedVector(
val
)(int (*f)(const
val
*, const
val
*),
size_t N);
Construct a sorted vector of elements of type val, using the comparison
function pointed to by
f
. The initial capacity of the vector will be
N
. The
capacity will automatically be increased should too many items be inserted.
#include <rw/gsortvec.h>
#include <rw/rstream.h>
declare(RWGVector,int)
declare(RWGSortedVector,int)
implement(RWGVector,int)
implement(RWGSortedVector,int)
// Declare and define the "comparison function":int compFun(const
int* a, const int* b)
{
return *a - *b;
}
main()
{
// Declare and define an instance,
// using the comparison function 'compFun':
RWGSortedVector(int) avec(compFun);
// Do some insertions:
avec.insert(3); // 3
avec.insert(17); // 3 17
avec.insert(5); // 3 5 17
cout < avec(1); // Prints '5'
cout < avec.index(17); // Prints '2'
}