Opened 21 years ago
Closed 15 years ago
#26 closed Feature Requests (wontfix)
mem_iter - iterator to object member — at Version 2
Reported by: | Owned by: | nobody | |
---|---|---|---|
Milestone: | Component: | None | |
Version: | None | Severity: | Showstopper |
Keywords: | Cc: |
Description (last modified by )
// 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<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
Change History (2)
comment:2 by , 15 years ago
Description: | modified (diff) |
---|---|
Reporter: | changed from | to
Resolution: | None → wontfix |
Severity: | → Showstopper |
Status: | assigned → closed |
I think this functionality is nicely covered by transform_iterator and mem_fn.
Note:
See TracTickets
for help on using tickets.