template<class Container, class Iter) insert_iterator<Container> inserter(Container& C, Iter i); | Equivalent to insert_iterator<Container>(C, i). [2] This is a global function, not a member function. |
Notes [1] Note the difference between assignment through a Container::iterator and assignment through an insert_iterator<Container>. If i is a valid Sequence::iterator, then it points to some particular element in the container; the expression *i = t replaces that element with t, and does not change the total number of elements in the container. If ii is a valid insert_iterator<container>, however, then the expression *ii = t is equivalent, for some container c and some valid container::iterator j, to the expression c.insert(j, t). That is, it does not overwrite any of c's elements and it does change c's size.
[2] Note how assignment through an insert_iterator is implemented. In general, unary operator* must be defined so that it returns a proxy object, where the proxy object defines operator= to perform the insert operation. In this case, for the sake of simplicity, the proxy object is the insert_iterator itself. That is, *i simply returns i, and *i = t is equivalent to i = t. You should not, however, rely on this behavior. It is an implementation detail, and it is not guaranteed to remain the same in future versions.
[3] This function exists solely for the sake of convenience: since it is a non-member function, the template parameters may be inferred and the type of the insert_iterator need not be declared explicitly. One easy way to reverse a range and insert it into a Sequence S, for example, is reverse_copy(first, last, inserter(S, S.begin())).
See also front_insert_iterator, back_insert_iterator, Output Iterator, Sequence, Iterator overview
reverse_iterator<RandomAccessIterator, T, Reference, Distance>
Categories: iterators, adaptors
Component type: type
Description Reverse_iterator is an iterator adaptor that enables backwards traversal of a range. Operator++ applied to an object of class reverse_iterator<RandomAccessIterator> means the same thing as operator-- applied to an object of class RandomAccessIterator. There are two different reverse iterator adaptors: the class reverse_iterator has a template argument that is a Random Access Iterator, and the class reverse_bidirectional_iterator has a template argument that is a Bidirectional Iterator. [1]
Example template <class T>
void forw(const vector<T>& V) {
vector<T>::iterator first = V.begin();
vector<T>::iterator last = V.end();
while (first != last) cout << *first++ << endl;
}
template <class T>
void rev(const vector<T>& V) {
typedef reverse_iterator<vector<T>::iterator, T, vector<T>::reference_type, vector<T>::difference_type> reverse_iterator; [2]
reverse_iterator rfirst(V.end());
reverse_iterator rlast(V.begin());
while (rfirst != rlast) cout << *rfirst++ << endl;
}
In the function forw, the elements are printed in the order *first, *(first+1) , …, *(last-1). In the function rev, they are printed in the order *(last – 1), *(last - 2), …, *first. [3]
Definition Defined in the standard header iterator, and in the nonstandard backward-compatibility header iterator.h.
Template parameters | Parameter | Description | Default |
RandomAccessIterator | The base iterator class. Incrementing an object of class reverse_iterator<Iterator> corresponds to decrementing an object of class Iterator. | |
T | The reverse iterator's value type. This should always be the same as the base iterator's value type. | |
Reference | The reverse iterator's reference type. This should always be the same as the base iterator's reference type. | T& |
Distance | The reverse iterator's distance type. This should always be the same as the base iterator's distance type. |