Subject: Re: [Boost-bugs] [Boost C++ Libraries] #26: mem_iter - iterator to object member
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2007-07-03 15:19:49
#26: mem_iter - iterator to object member
-------------------------------------+--------------------------------------
Reporter: peter.andrews_at_[hidden] | Owner: nobody
Type: Feature Requests | Status: closed
Milestone: | Component: None
Version: None | Severity: Showstopper
Resolution: wontfix | Keywords:
-------------------------------------+--------------------------------------
Changes (by dave):
* status: assigned => closed
* severity: => Showstopper
* resolution: None => wontfix
* reporter: nobody => peter.andrews_at_[hidden]
Old description:
> {{{
> // Submitted by Peter Andrews - andrews_at_[hidden]
> //
> // 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<employee*> 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(),
> &employee::salary ),
> mem_iter( slackers.end(),
> &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
>
> }}}
New description:
{{{
// Submitted by Peter Andrews - andrews_at_[hidden]
//
// 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<employee*> 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(),
&employee::salary ),
mem_iter( slackers.end(),
&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
}}}
Comment:
I think this functionality is nicely covered by transform_iterator and
mem_fn.
--
Ticket URL: <http://svn.boost.org/trac/boost/ticket/26#comment:2>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.
This archive was generated by hypermail 2.1.7 : 2017-02-16 18:49:55 UTC