Opened 16 years ago

Closed 10 years ago

#26 closed Feature Requests (wontfix)

mem_iter - iterator to object member

Reported by: peter.andrews@… Owned by: nobody
Milestone: Component: None
Version: None Severity: Showstopper
Keywords: Cc:

Description (last modified by Dave Abrahams)

// Submitted by Peter Andrews -
// 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 

Attachments (0)

Change History (2)

comment:1 Changed 16 years ago by nobody

Logged In: NO 

I could not upload the files for some reason -
email for a copy.

comment:2 Changed 10 years ago by Dave Abrahams

Description: modified (diff)
Reporter: changed from nobody to peter.andrews@…
Resolution: Nonewontfix
Severity: Showstopper
Status: assignedclosed

I think this functionality is nicely covered by transform_iterator and mem_fn.

Modify Ticket

Change Properties
Set your email in Preferences
as closed The owner will remain nobody.
The resolution will be deleted.

Add Comment

E-mail address and name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.