Reverse inserter

64 views
Skip to first unread message

NDos Dannyu

unread,
Mar 6, 2016, 12:16:17 AM3/6/16
to ISO C++ Standard - Future Proposals
std::inserter uses the plain iterator of the container, so it can't be used to insert elements in reverse order.
std::insert_iterator can't be passed to std::make_reverse_iterator, because it has void member types.
So I suggest adding reverse inserter, Like this:

namespace std {
    template <class Container> class reverse_insert_iterator {
    private:
        typedef typename Container::iterator __i;
    protected:
        Container *container;
        __i iter;
    public:
        typedef Container container_type;
        typedef void value_type, difference_type, pointer, reference;
        typedef output_iterator_tag iterator_category;
        explicit reverse_insert_iterator(Container &c, __i i) noexcept :
            container(&c), iter(make_reverse_iterator(i)) {}
        reverse_insert_iterator &operator = (const typename Container::value_type &value) {
            iter = container->insert(iter, value);
            // Note that iter isn't increased here and thus simulates reverse insertion.
            return *this;
        }
        reverse_insert_iterator &operator = (typename Container::value_type &&value) {
            iter = container->insert(iter, value);
            // Ditto.
            return *this;
        }
        reverse_insert_iterator &operator * ()  noexcept {
            return *this;
        }
        reverse_insert_iterator &operator ++ () noexcept {
            return *this;
        }
        reverse_insert_iterator &operator ++ (int) noexcept {
            return *this;
        }
    };
    template <class Container>
    reverse_insert_iterator<Container> reverse_inserter(Container &c, typename Container::iterator i) noexcept {
        return reverse_insert_iterator<Container>(c, i);
    }
}

Arthur O'Dwyer

unread,
Mar 7, 2016, 2:06:58 AM3/7/16
to ISO C++ Standard - Future Proposals
On Saturday, March 5, 2016 at 9:16:17 PM UTC-8, NDos Dannyu wrote:
std::inserter uses the plain iterator of the container, so it can't be used to insert elements in reverse order.

I imagine that most of the use-cases for such a thing could be handled with std::front_inserter.

IIUC, you're looking specifically for a way to
- insert elements from the range B...E
- into an existing (possibly non-empty) container
- not at the front, but rather somewhere in the middle (otherwise front_inserter would fit the bill)
- where the inserted elements must wind up in reverse order
- where for some reason inserting the elements "in non-reversed order, from the range make_reverse_iterator(E)...make_reverse_iterator(B)" would be unacceptable

and you really want to use a standard algorithm for this instead of writing a for loop.

I suspect that it wouldn't be worth adding to the STL's zoo of weird iterator types, given that Ranges are going to arrive soon. How does Ranges handle insertion?

my $.02,
Arthur
Reply all
Reply to author
Forward
0 new messages