On Thu, Apr 29, 2010 at 8:20 AM, Magnus Fromreide <ma...@lysator.liu.se>wrote:

> On Thu, 2010-04-29 at 03:31 +0000, rst...@users.sourceforge.net wrote:
> > Revision: 18623
> >
> http://net-snmp.svn.sourceforge.net/net-snmp/?rev=18623&view=rev
> > Author:   rstory
> > Date:     2010-04-29 03:31:29 +0000 (Thu, 29 Apr 2010)
> >
> > Log Message:
> > -----------
> > NEWS: snmplib: add remove function to container iterator; implement it
> for
> >  binary_array
>
> You choose to let iterator::remove move to the previous entry. I assume
> this means you are ruling out the possibility of a single-linked list?
>

Hello Robert,

Because not all containers support iterating backward, some container
implementations let the erase function return an iterator to the next entry
instead of the previous. The following example uses the C++ STL and shows
that even with an erase function that returns a pointer to the next entry it
is still possible to implement erase functions in an elegant way (see also
the erase_value() function):

#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>

template<typename T>
inline void print_container(const T& container)
{
    std::copy(container.begin(), container.end(),
              std::ostream_iterator<typename T::value_type>(std::cout, "
"));
    std::cout << '\n';
}

template <typename T, typename U>
inline void erase_value(T& container, const U& value)
{
    typename T::iterator next;
    for (typename T::iterator p = container.begin(); p != container.end();
         p = next)
    {
        next = p;
        ++next;
        if (*p == value)
            next = container.erase(p);
    }
}

int main()
{
    std::list<int> list;
    list.push_back(7);
    list.push_back(3);
    list.push_back(5);
    print_container(list);
    erase_value(list, 3);
    print_container(list);
    return 0;
}

The output of the above program is as follows:

7 3 5
7 5

Note: I know about erase_if(), but didn't use this function on purpose.

Bart.
------------------------------------------------------------------------------
_______________________________________________
Net-snmp-coders mailing list
Net-snmp-coders@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders

Reply via email to