| &*(ri.base() – 1). |
reverse_bidirectional_iterator(BidirectionalIterator i) | Constructs a reverse_bidirectional_iterator whose base iterator is i. |
Notes [1] There isn't really any good reason to have two separate classes: this separation is purely because of a technical limitation in some of today's C++ compilers. If the two classes were combined into one, then there would be no way to declare the return types of the iterator tag functions iterator_category, distance_type and value_type correctly. The iterator traits class solves this problem: it addresses the same issues as the iterator tag functions, but in a cleaner and more flexible manner. Iterator traits, however, rely on partial specialization, and many C++ compilers do not yet implement partial specialization. Once compilers that support partial specialization become more common, these two different reverse iterator classes will be combined into a single class.
[2] The declarations for rfirst and rlast are written in this clumsy form simply as an illustration of how to declare a reverse_bidirectional_iterator. List is a Reversible Container, so it provides a typedef for the appropriate instantiation of reverse_bidirectional_iterator. The usual way of declaring these variables is much simpler:
list<T>::reverse_bidirectional_iterator rfirst = rbegin();
list<T>::reverse_bidirectional_iterator rlast = rend();
[3] Note the implications of this remark. The variable rfirst is initialized as reverse_bidirectional_iterator<…> rfirst(V.end());. The value obtained when it is dereferenced, however, is *(V.end() – 1). This is a general property: the fundamental identity of reverse iterators is &*(reverse_bidirectional_iterator(i)) == &*(i – 1) . This code sample shows why this identity is important: if [f, l) is a valid range, then it allows [reverse_bidirectional_iterator(l), reverse_bidirectional_iterator(f)) to be a valid range as well. Note that the iterator l is not part of the range, but it is required to be dereferenceable or past-the-end. There is no requirement that any such iterator precedes f.
See also Reversible Container, reverse_iterator, Bidirectional Iterator, iterator tags, Iterator overview
raw_storage_iterator<ForwardIterator, T>
Categories: allocators, iterators, adaptors
Component type: type
Description In C++, the operator new allocates memory for an object and then creates an object at that location by calling a constructor. Occasionally, however, it is useful to separate those two operations. [1] If i is an iterator that points to a region of uninitialized memory, then you can use construct to create an object in the location pointed to by i. Raw_storage_iterator is an adaptor that makes this procedure more convenient. If r is a raw_storage_iterator, then it has some underlying iterator i. The expression *r = x is equivalent to construct(&*i, x).
Example class Int {
public:
Int(int x) : val(x) {}
int get() { return val; }
private:
int val;
};
int main() {
int A1[] = {1, 2, 3, 4, 5, 6, 7};
const int N = sizeof(A1) / sizeof(int);
Int* A2 = (Int*)malloc(N * sizeof(Int));
transform(A1, A1 + N, raw_storage_iterator<Int*, int>(A2), negate<int> ());
}
Definition Defined in the standard header memory, and in the nonstandard backward-compatibility header iterator.h.
Template parameters | Parameter | Description |
| OutputIterator | The type of the raw_storage_iterator's underlying iterator. |
T | The type that will be used as the argument to the constructor. |
Model of Output Iterator
Type requirements • ForwardIterator is a model of Forward Iterator
• ForwardIterator's value type has a constructor that takes a single argument of type T.
Public base classes None.
Members