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

©Copyright 1996 Rogue Wave Software

RWTPtrMap<K,T,C>

Synopsis

#include <rw/tpmap.h> 
RWTPtrMap<K,T,C> m;

Standard C++ Library Dependent!


RWTPtrMap requires the Standard C++ Library.


Description

This class maintains a pointer-based collection of associations of type pair<K* const, T*>. The first part of the association is a key of type K*, the second is its associated item of type T*. Order is determined by the key according to a comparison object of type C. C must induce a total ordering on elements of type K via a public member

bool operator()(const K& x, const K& y)

which returns true if x and its partner should precede y and its partner within the collection. The structure less<T> from the C++-standard header file <functional> is an example. Note that keys will be dereferenced before being compared.

RWTPtrMap<K,T,C> will not accept a key that compares equal to any key already in the collection. (RWTPtrMultiMap<K,T,C> may contain multiple keys that compare equal to each other.) Equality is based on the comparison object and not on the == operator. Given a comparison object comp, keys a and b are equal if

!comp(a,b) && !comp(b,a).

Persistence

Isomorphic.

Examples

In this example, a map of RWCStrings and RWDates is exercised.

//
// tpmap.cpp
//
#include <rw/tpmap.h>
#include <rw/cstring.h>
#include <rw/rwdate.h>
#include <iostream.h>
#include <function.h>

main(){
  RWTPtrMap<RWCString, RWDate, less<RWCString> > birthdays;

  birthdays.insert
   (
     new RWCString("John"),
     new RWDate(12, "April", 1975)
   );
  birthdays.insert
   (
     new RWCString("Ivan"),
     new RWDate(2, "Nov", 1980)
   );

  // Alternative syntax:
  birthdays[new RWCString("Susan")] = 
    new RWDate(30, "June", 1955);
  birthdays[new RWCString("Gene")] =
    new RWDate(5, "Jan", 1981);

  // Print a birthday:
  RWCString key("John");
  cout << *birthdays[&key] << endl;
  return 0;
}

Program Output:
04/12/75

Related Classes

Class RWTPtrMultiMap<K,T,C> offers the same interface to a pointer-based collection that accepts multiple keys that compare equal to each other. RWTPtrSet<T,C> maintains a pointer-based collection of keys without the associated items.

Class map<K*,T*,deref_compare<C,K, allocator> > is the C++-standard collection that serves as the underlying implementation for this collection.

Public Typedefs

typedef rw_deref_compare<C,K>                  container_comp;
typedef map<K*,T*,container_comp, allocator>   container_type;
typedef container_type::size_type              size_type;
typedef container_type::difference_type        difference_type;
typedef container_type::iterator               iterator;
typedef container_type::const_iterator         const_iterator;
typedef pair <K* const, T*>                    value_type;
typedef pair <K* const, T*>&                   reference;
typedef const pair <K* const, T*>&             const_reference;
typedef K*                                     value_type_key;
typedef T*                                     value_type_data;
typedef K*&                                    reference_key;
typedef T*&                                    reference_data;
typedef const K*const&                   const_reference_key;
typedef const T*const&                   const_reference_data;

Public Constructors

RWTPtrMap<K,T,C>
(const container_comp& comp = container_comp());
RWTPtrMap<K,T,C>(const RWTPtrMap<K,T,C>& rwm);
RWTPtrMap<K,T,C>(const container_type& m);
RWTPtrMap<K,T,C>
(value_type* first,value_type* last,
 const container_comp& comp = container_comp());

Public Member Operators

RWTPtrMap<K,T,C>&
operator=(const RWTPtrMap<K,T,C>& m);
RWTPtrMap<K,T,C>&
operator=(const container_type& m);
bool
operator<(const RWTPtrMap<K,T,C>& m) const;
bool
operator==(const RWTPtrMap<K,T,C>& m) const;
T*&
operator[](const K* key);

Public Member Functions

void
apply(void (*fn)(const K*,T*&,void*),void* d);
void
apply(void (*fn)(const K*,const T*,void*),void* d) const;
void
applyToKeyAndValue(void (*fn)(const K*,T*&,void*),void* d);
void
applyToKeyAndValue
(void (*fn)(const K*,const T*,void*),void* d) const;
iterator
begin();
const_iterator
begin() const;
void
clear();
void
clearAndDestroy();
bool
contains(const K* key) const;
bool
contains(bool (*fn)(value_type,void*), void* d) const;
iterator
end();
const_iterator
end() const;
size_type
entries() const;
const K*
find(const K* key) const;
value_type
find(bool (*fn)(value_type,void*), void* d) const;
T*
findValue(const K* key);
const T*
findValue(const K* key) const;
const K*
findKeyAndValue(const K* key, T*& tr);
const K*
findKeyAndValue(const K* key, const T*& tr) const;
bool
insert(K* key, T* a);
bool
insertKeyAndValue(K* key, T* a);
bool
isEmpty() const;
size_type
occurrencesOf(const K* key) const;
size_type
occurrencesOf
(bool (*fn)(value_type,void*), void* d) const;
K*
remove(const K* key);
K*
remove(bool (*fn)(value_type,void*), void* d);
size_type
removeAll(const K* key);
size_type
removeAll(bool (*fn)(value_type,void*), void* d);
container_type
std();
const container_type
std() const;

Related Global Operations

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