1 /** \page CodeSnippets Code Snippets
4 If you prefer using iterator in a \c for loop, but dislike to figure out
5 the exact type of the iterator, you may find the \c for_ macro convenient:
7 #include "zypp/base/Easy.h"
9 for_( it, pool.byIdentBegin( kind, name ),
10 pool.byIdentEnd( kind, name ) )
17 for ( ResPool::byIdent_iterator it = pool.byIdentBegin( kind, name ),
18 end = pool.byIdentEnd( kind, name );
25 \section erase erase elements from containers
27 // //////////////////////////////////////////////////////////////////////
28 // Avoid buggy code, that tries to erase elements, matching a
29 // certain property from containers. Example:
31 // for (ResStore::iterator it = store.begin(); it != store.end(); ++it)
36 // Problem: Removing an element from a container invalidates (at least)
37 // all iterators pointing to it. Thus after erasing *it, it is
38 // no longer valid. ++it has UNDEFINED BEHAVIOUR.
40 // //////////////////////////////////////////////////////////////////////
41 // Loop based algorithms (differs depending on the kind of container)
42 // =====================
43 // //////////////////////////////////////////////////////////////////////
45 // //////////////////////////////////////////////////////////////////////
46 // Sequential container (vector string deque list): erase returns
47 // a valid iterator to the next element.
48 // //////////////////////////////////////////////////////////////////////
51 for ( SeqContainer::iterator it = c.begin(); it != c.end(); /**/ )
53 if ( toBeRemoved( *it ) )
55 it = c.erase( it ); // valid next-iterator returned
62 // //////////////////////////////////////////////////////////////////////
63 // Associative container (maps sets): erase returns void, but we can use
64 // postfix increment, as ONLY iterators to the eased object get invalid:
65 // //////////////////////////////////////////////////////////////////////
68 for ( AssocContainer::iterator it = c.begin(); it != c.end(); /**/ )
70 if ( toBeRemoved( *it ) )
72 c.erase( it++ ); // postfix! Incrementing before erase
79 // //////////////////////////////////////////////////////////////////////
83 // In case toBeRemoved above is actually a function/functor.
84 // //////////////////////////////////////////////////////////////////////
87 // //////////////////////////////////////////////////////////////////////
88 // Sequential container (vector string deque): stl::remove_if,
89 // does not erase elements, they are just moved to the containers
90 // end, and an iterator to the 1st item to be 'removed' is returned.
91 // //////////////////////////////////////////////////////////////////////
94 c.erase( stl::remove_if( c.begin(), c.end(), toBeRemoved ),
98 // //////////////////////////////////////////////////////////////////////
99 // Sequential container (list): The above works too, but list has a
100 // builtin remove/remove_if which is more efficient.
101 // //////////////////////////////////////////////////////////////////////
104 c.remove_if( toBeRemoved );
107 // //////////////////////////////////////////////////////////////////////
108 // Associative container (maps sets): Actually the loop above is the most
109 // efficient solution. There is an algorithm based solution, but it requires
110 // copying all elements not to be removed ;(
111 // //////////////////////////////////////////////////////////////////////
115 AssocContainer keepItems;
116 stl::remove_copy_if( c.begin(), c.end(),
117 stl::inserter( keepItems, keepItems.end() ),