RWTValDeque(3C++) RWTValDeque(3C++)
Name
RWTValDeque<T> - Rogue Wave library class
Synopsis
#include <rw/tvdeque.h>
RWTValDeque<T> deq;
Standard C++ Library Dependent!RWTValDeque requires the Standard C++ Library.
Description
This class maintains a collection of values implemented as a double-ended
queue, or deque. Order is determined externally and elements are
accessible by index. Use this class when insertions and deletions
usually occur at either the beginning or the end of the collection.
Persistence
Isomorphic
Example
In this example, a double-ended queue of ints is exercised.
//
// tvdqint.cpp
//
#include <rw/tvdeque.h>
#include <iostream.h>
/*
* This program partitions integers into even and odd numbers
*/
int main(){
RWTValDeque<int> numbers;
int n;
cout << "Input an assortment of integers (EOF to end):"
Page 1
RWTValDeque(3C++) RWTValDeque(3C++)
<< endl;
while (cin >> n) {
if (n % 2 == 0)
numbers.pushFront(n);
else
numbers.pushBack(n);
}
while (numbers.entries()) {
cout << numbers.popFront() << endl;
}
return 0;
}
Program Input:
1 2 3 4 5
<eof>
Program Output:
4
2
1
3
5
Related Classes
Classes RWTValSlist<T>, RWTValDlist<T>, RWTValSortedDlist<T>, and
RWTValOrderedVector<T> also provide a Rogue Wave interface to C++-
standard sequence collections. The list classes should be considered for
frequent insertions (or removals) in the interior of the collection. The
vector may be more efficient if most insertions and removals occur at the
end of the collection. Class deque<T,allocator> is the C++-standard
collection that serves as the underlying implementation for this class.
Public Typedefs
typedef deque<T,allocator> container_type;
typedef container_type::iterator iterator;
typedef container_type::const_iterator const_iterator;
typedef container_type::size_type size_type;
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
Public Constructors
RWTValDeque<T>();
Constructs an empty, double-ended queue.
Page 2
RWTValDeque(3C++) RWTValDeque(3C++)
RWTValDeque<T>(const deque<T,allocator>& deq);
Constructs a double-ended queue by copying all elements of deq.
RWTValDeque<T>(const RWTValDeque<T>& rwdeq);
Copy constructor.
RWTValDeque<T>(size_type n, const T& val = T());
Constructs a double-ended queue with n elements, each initialized to val.
RWTValDeque<T>(const T* first, const T* last);
Constructs a double-ended queue by copying elements from the array of Ts
pointed to by first, up to, but not including, the element pointed to by
last.
Public Member Operators
RWTValDeque<T>&
operator=(const RWTValDeque<T,allocator>& deq);
RWTValDeque<T>&
operator=(const deque<T>& deq);
Calls the destructor on all elements of self and replaces them by copying
all elements of deq.
bool
operator<(const RWTValDeque<T>& deq) const;
bool
operator<(const deque<T,allocator>& deq) const;
Returns true if self compares lexicographically less than deq, otherwise
returns false. Type T must have well-defined less-than semantics
(T::operator<(const T&) or equivalent).
bool
operator==(const RWTValDeque<T>& deq) const;
bool
operator==(const deque<T,allocator>& deq) const;
Returns true if self compares equal to deq, otherwise returns false. Two
collections are equal if both have the same number of entries, and
iterating through both collections produces, in turn, individual elements
that compare equal to each other.
Page 3
RWTValDeque(3C++) RWTValDeque(3C++)
reference
operator()(size_type i);
const_reference
operator()(size_type i) const;
Returns a reference to the ith element of self. Index i should be
between 0 and one less then the number of entries, otherwise the results
are undefined--no bounds checking is performed.
reference
operator[](size_type i);
const_reference
operator[](size_type i) const;
Returns a reference to the ith element of self. Index i must be between
0 and one less then the number of entries in self, otherwise the
function throws an exception of type RWBoundsErr.
Public Member Functions
void
append(const_reference a);
Adds the item a to the end of the collection.
void
apply(void (*fn)(reference,void*), void* d);
void
apply(void (*fn)(const_reference,void*), void* d) const;
Applies the user-defined function pointed to by fn to every item in the
collection. This function must have one of the prototypes:
void yourfun(const_reference a, void* d);
void yourfun(reference a, void* d);
Client data may be passed through parameter d.
reference
at(size_type i);
const_reference
at(size_type i) const;
Page 4
RWTValDeque(3C++) RWTValDeque(3C++)
Returns a reference to the ith element of self. Index i must be between
0 and one less then the number of entries in self, otherwise the
function throws an exception of type RWBoundsErr.
iterator
begin();
const_iterator
begin() const;
Returns an iterator positioned at the first element of self.
void
clear();
Clears the collection by removing all items from self. Each item will
have its destructor called.
bool
contains(const_reference a) const;
Returns true if there exists an element t in self such that the
expression(t == a) is true, otherwise returns false.
bool
contains(bool (*fn)(const_reference,void*), void* d) const;
Returns true if there exists an element t in self such that the
expression ((*fn)(t,d)) is true, otherwise returns false. fn points to a
user-defined tester function which must have prototype:
bool yourTester(const_reference a, void* d);
Client data may be passed through parameter d.
iterator
end();
const_iterator
end() const;
Returns a past-the-end valued iterator of self.
Page 5
RWTValDeque(3C++) RWTValDeque(3C++)
size_type
entries() const;
Returns the number of elements in self.
bool
find(const_reference a,T& k) const;
If there exists an element t in self such that the expression (t == a) is
true, assigns t to k and returns true. Otherwise, returns false and
leaves the value of k unchanged.
bool
find(bool (*fn)(const_reference,void*), void* d, T& k) const;
If there exists an element t in self such that the expression
((*fn)(t,d)) is true, assigns t to k and returns true. Otherwise,
returns false and leaves the value of k unchanged. fn points to a user-
defined tester function which must have prototype:
bool yourTester(const_reference a, void* d);
Client data may be passed through parameter d.
reference
first();
const_reference
first() const;
Returns a reference to the first element of self.
size_type
index(const_reference a) const;
Returns the position of the first item t in self such that (t == a), or
returns the static member npos if no such item exists.
size_type
index(bool (*fn)(const_reference,void*), void* d) const;
Returns the position of the first item t in self such that((*fn)(t,d)) is
Page 6
RWTValDeque(3C++) RWTValDeque(3C++)
true, or returns the static member npos if no such item exists. fn
points to a user-defined tester function which must have prototype:
bool yourTester(const_reference a, void* d);
Client data may be passed through parameter d.
bool
insert(const_reference a);
Adds the item a to the end of the collection. Returns true.
void
insertAt(size_type i, const_reference a);
Inserts the item a in front of the item at position i in self. This
position must be between 0 and the number of entries in the collection,
otherwise the function throws an exception of type RWBoundsErr.
bool
isEmpty() const;
Returns true if there are no items in the collection, false otherwise.
reference
last();
const_reference
last() const;
Returns a reference to the last item in the collection.
reference
maxElement();
const_reference
maxElement() const;
reference
minElement();
const_reference
minElement() const;
Returns a reference to the minimum or maximum element in the collection.
Type T must have well-defined less-than semantics (T::operator<(const T&)
Page 7
RWTValDeque(3C++) RWTValDeque(3C++)
or equivalent).
size_type
occurrencesOf(const_reference a) const;
Returns the number of elements t in self such that the expression (t ==
a) is true.
size_type
occurrencesOf(bool (*fn)(const_reference,void*),void* d) const;
Returns the number of elements t in self such that the expression
((*fn)(t,d)) is true. fn points to a user-defined tester function which
must have prototype:
bool yourTester(const_reference a, void* d);
Client data may be passed through parameter d.
void
prepend(const_reference a);
Adds the item a to the beginning of the collection.
T
popBack();
Removes and returns the last item in the collection.
T
popFront();
Removes and returns the first item in the collection.
void
pushBack(const_reference a);
Adds the item a to the end of the collection.
Page 8
RWTValDeque(3C++) RWTValDeque(3C++)
void
pushFront(const_reference a);
Adds the item a to the beginning of the collection.
bool
remove(const_reference a);
Removes the first element t in self such that the expression (t == a) is
true and returns true. Returns false if there is no such element.
bool
remove(bool (*fn)(const_reference,void*), void* d);
Removes the first element t in self such that the expression ((*fn)(t,d))
is true and returns true. Returns false if there is no such element. fn
points to a user-defined tester function which must have prototype:
bool yourTester(const_reference a, void* d);
Client data may be passed through parameter d.
size_type
removeAll(const_reference a);
Removes all elements t in self such that the expression (t == a) is true.
Returns the number of items removed.
size_type
removeAll(bool (*fn)(const_reference,void*), void* d);
Removes all elements t in self such that the expression ((*fn)(t,d)) is
true. Returns the number of items removed. fn points to a user-defined
tester function which must have prototype:
bool yourTester(const_reference a, void* d);
Page 9
RWTValDeque(3C++) RWTValDeque(3C++)
Client data may be passed through parameter d.
T
removeAt(size_type i);
Removes and returns the item at position i in self. This position must
be between 0 and one less then the number of entries in the collection,
otherwise the function throws an exception of type RWBoundsErr.
T
removeFirst();
Removes and returns the first item in the collection.
T
removeLast();
Removes and returns the first item in the collection.
size_type
replaceAll(const T& oldVal, const T& newVal);
Replaces all elements t in self such that the expression (t == oldVal) is
true with newVal. Returns the number of items replaced.
size_type
replaceAll(bool (*fn)(const T&,void*), void* d,
const T& newVal);
Replaces all elements t in self such that the expression ((*fn)(t,d))is
true.with newVal Returns the number of items replaced. fn points to a
user-defined tester function which must have prototype:
bool yourTester(const T& a, void* d);
Client data may be passed through parameter d.
void
sort();
Sorts the collection using the less-than operator (<) to compare
Page 10
RWTValDeque(3C++) RWTValDeque(3C++)
elements.
deque<T,allocator>&
std();
const deque<T,allocator>&
std() const;
Returns a reference to the underlying C++-standard collection that serves
as the implementation for self. This reference may be used freely,
providing access to the C++-standard interface as well as
interoperability with other software components that make use of C++-
standard collections.
Static Public Data Member
const size_type npos;
This is the value returned by member functions such as index to indicate
a non-position. The value is equal to ~(size_type)0.
Related Global Operators
RWvostream&
operator<<(RWvostream& strm, const RWTValDeque<T>& coll);
RWFile&
operator<<(RWFile& strm, const RWTValDeque<T>& coll);
Saves the collection coll onto the output stream strm, or a reference to
it if it has already been saved.
RWvistream&
operator>>(RWvistream& strm, RWTValDeque<T>& coll);
RWFile&
operator>>(RWFile& strm, RWTValDeque<T>& coll);
Restores the contents of the collection coll from the input stream strm.
RWvistream&
operator>>(RWvistream& strm, RWTValDeque<T>*& p);
RWFile&
operator>>(RWFile& strm, RWTValDeque<T>*& p);
Looks at the next object on the input stream strm and either creates a
new collection off the heap and sets p to point to it, or sets p to point
to a previously read instance. If a collection is created off the heap,
then you are responsible for deleting it.
Page 11