Boost C++ Libraries: Ticket #26: mem_iter - iterator to object member https://svn.boost.org/trac10/ticket/26 <pre class="wiki">// Submitted by Peter Andrews - andrews@hepmail.physics.yale.edu // // Sometimes you have a container of objects, and want to access some object member // using iterators in a generic algorithm. For example, struct employee { // please ignore encapsulation issues... const string name; long int age(); short int salary; // other stuff... } list&lt;employee*&gt; slackers; // Assume below that all_emp container and predicate is_a_slacker exist... copy_if( all_emp.begin(), all_emp.end(), back_inserter( slackers ), is_a_slacker ); // Now you might want to count up all slacker's salaries... Assume functor add_salaries exists. int wasted_money = accumulate( slackers.begin(), slackers.end(), 0, add_salaries ); // #### The problem above is that you need to write the add_salaries functor #### // This can be quite a hassle if you want to access a lot of different members, using a lot of // different algorithms. // // I have written some code which simplifies dealing with members through // iterators in generic algorithms. In the example above, you would write: int wasted_money = accumulate( mem_iter( slackers.begin(), &amp;employee::salary ), mem_iter( slackers.end(), &amp;employee::salary ), 0 ); // Above, mem_iter takes the container iterator and pointer to member, and returns // an iterator which appears to directly reference the salary member of employee. // Your algorithms are none the wiser, and you are saved from writing an employee and // salary specific functor when all you wanted to do was add up some salaries // (while shunning explicit loops). // In addition to mem_iter for containers of pointers to objects, I wrote mem_iter_ref // for containers of objects, fun_iter and fun_iter_ref for no argument member functions. // Please let me know what you think of this. // Please let me know if something like this exists already. // Please let me know how I can improve my sloppy implementation of it. // Please let me know if it is proper to introduce this idea here. // I will try to upload my sample files which demonstrate mem_iter. // There are three header files ( two are not central to the concept, but need inclusion ) // There is one source file to test the usage of mem_iter. // Email me if you need copies of these files </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/26 Trac 1.4.3 nobody Fri, 14 Dec 2001 22:49:33 GMT <link>https://svn.boost.org/trac10/ticket/26#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/26#comment:1</guid> <description> <pre class="wiki">Logged In: NO I could not upload the files for some reason - email peter.andrews@yale.edu for a copy. </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Dave Abrahams</dc:creator> <pubDate>Tue, 03 Jul 2007 15:19:49 GMT</pubDate> <title>status, resolution, description, reporter changed; severity set https://svn.boost.org/trac10/ticket/26#comment:2 https://svn.boost.org/trac10/ticket/26#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>severity</strong> → <span class="trac-field-new">Showstopper</span> </li> <li><strong>resolution</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">wontfix</span> </li> <li><strong>description</strong> modified (<a href="/trac10/ticket/26?action=diff&amp;version=2">diff</a>) </li> <li><strong>reporter</strong> changed from <span class="trac-author">nobody</span> to <span class="trac-author">peter.andrews@…</span> </li> </ul> <p> I think this functionality is nicely covered by transform_iterator and mem_fn. </p> Ticket