<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <meta http-equiv="X-UA-Compatible" content="IE=9"/> <meta name="generator" content="Doxygen 1.8.15"/> <meta name="viewport" content="width=device-width, initial-scale=1"/> <title>libzypp: Code Snippets</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="dynsections.js"></script> <link href="navtree.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="resize.js"></script> <script type="text/javascript" src="navtreedata.js"></script> <script type="text/javascript" src="navtree.js"></script> <script type="text/javascript"> /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */ $(document).ready(initResizable); /* @license-end */</script> <link href="doxygen.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="top"><!-- do not remove this div, it is closed by doxygen! --> <div id="titlearea"> <table cellspacing="0" cellpadding="0"> <tbody> <tr style="height: 56px;"> <td id="projectalign" style="padding-left: 0.5em;"> <div id="projectname">libzypp  <span id="projectnumber">17.9.0</span> </div> </td> </tr> </tbody> </table> </div> <!-- end header part --> <!-- Generated by Doxygen 1.8.15 --> <script type="text/javascript" src="menudata.js"></script> <script type="text/javascript" src="menu.js"></script> <script type="text/javascript"> /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */ $(function() { initMenu('',false,false,'search.php','Search'); }); /* @license-end */</script> <div id="main-nav"></div> </div><!-- top --> <div id="side-nav" class="ui-resizable side-nav-resizable"> <div id="nav-tree"> <div id="nav-tree-contents"> <div id="nav-sync" class="sync"></div> </div> </div> <div id="splitbar" style="-moz-user-select:none;" class="ui-resizable-handle"> </div> </div> <script type="text/javascript"> /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */ $(document).ready(function(){initNavTree('CodeSnippets.html','');}); /* @license-end */ </script> <div id="doc-content"> <div class="PageDoc"><div class="header"> <div class="headertitle"> <div class="title">Code Snippets </div> </div> </div><!--header--> <div class="contents"> <div class="textblock"><h1><a class="anchor" id="for_"></a> for_</h1> <p>If you prefer using iterator in a <code>for</code> loop, but dislike to figure out the exact type of the iterator, you may find the <code>for_</code> macro convenient: </p><div class="fragment"><div class="line"><span class="preprocessor">#include "<a class="code" href="Easy_8h.html">zypp/base/Easy.h</a>"</span></div><div class="line"></div><div class="line"><a class="code" href="Easy_8h.html#a7887338b8a0727becb37d8061fcbfc0e">for_</a>( it, pool.byIdentBegin( kind, name ),</div><div class="line"> pool.byIdentEnd( kind, name ) )</div><div class="line">{</div><div class="line"> PoolItem <a class="code" href="namespacezypp_1_1iostr.html#a3807dcc209ee089a23589f70c7830b27">copy</a> = *it;</div><div class="line">}</div></div><!-- fragment --><p> instead of: </p><div class="fragment"><div class="line"><span class="keywordflow">for</span> ( ResPool::byIdent_iterator it = pool.byIdentBegin( kind, name ),</div><div class="line"> end = pool.byIdentEnd( kind, name );</div><div class="line"> it != end, ++it )</div><div class="line">{</div><div class="line"> PoolItem <a class="code" href="namespacezypp_1_1iostr.html#a3807dcc209ee089a23589f70c7830b27">copy</a> = *it;</div><div class="line">}</div></div><!-- fragment --><h1><a class="anchor" id="erase"></a> erase elements from containers</h1> <pre class="fragment"> // ////////////////////////////////////////////////////////////////////// // Avoid buggy code, that tries to erase elements, matching a // certain property from containers. Example: // // for (ResStore::iterator it = store.begin(); it != store.end(); ++it) // { // _pool.erase(*it); // } // // Problem: Removing an element from a container invalidates (at least) // all iterators pointing to it. Thus after erasing *it, it is // no longer valid. ++it has UNDEFINED BEHAVIOUR. // ////////////////////////////////////////////////////////////////////// // Loop based algorithms (differs depending on the kind of container) // ===================== // ////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////// // Sequential container (vector string deque list): erase returns // a valid iterator to the next element. // ////////////////////////////////////////////////////////////////////// SeqContainer c; for ( SeqContainer::iterator it = c.begin(); it != c.end(); /**/ ) { if ( toBeRemoved( *it ) ) { it = c.erase( it ); // valid next-iterator returned } else ++it; } // ////////////////////////////////////////////////////////////////////// // Associative container (maps sets): erase returns void, but we can use // postfix increment, as ONLY iterators to the eased object get invalid: // ////////////////////////////////////////////////////////////////////// AssocContainer c; for ( AssocContainer::iterator it = c.begin(); it != c.end(); /**/ ) { if ( toBeRemoved( *it ) ) { c.erase( it++ ); // postfix! Incrementing before erase } else ++it; } // ////////////////////////////////////////////////////////////////////// // stl algorithms // ============== // // In case toBeRemoved above is actually a function/functor. // ////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////// // Sequential container (vector string deque): stl::remove_if, // does not erase elements, they are just moved to the containers // end, and an iterator to the 1st item to be 'removed' is returned. // ////////////////////////////////////////////////////////////////////// SeqContainer c; c.erase( stl::remove_if( c.begin(), c.end(), toBeRemoved ), c.end() ); // ////////////////////////////////////////////////////////////////////// // Sequential container (list): The above works too, but list has a // builtin remove/remove_if which is more efficient. // ////////////////////////////////////////////////////////////////////// list c; c.remove_if( toBeRemoved ); // ////////////////////////////////////////////////////////////////////// // Associative container (maps sets): Actually the loop above is the most // efficient solution. There is an algorithm based solution, but it requires // copying all elements not to be removed ;( // ////////////////////////////////////////////////////////////////////// AssocContainer c; AssocContainer keepItems; stl::remove_copy_if( c.begin(), c.end(), stl::inserter( keepItems, keepItems.end() ), toBeRemoved ); c.swap( keepItems );</pre> </div></div><!-- PageDoc --> </div><!-- contents --> </div><!-- doc-content --> <!-- start footer part --> <div id="nav-path" class="navpath"><!-- id is needed for treeview function! --> <ul> <li class="footer">Generated by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.15 </li> </ul> </div> </body> </html>