Thread: help with a functor

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    6

    help with a functor

    i'll keep it short, hope this doesn't sound lazy:
    i need to make a functor that can act upon any stl container when given an iterator.
    best analog that comes to mind is std::make_heap.

    i just cant wrap my head around how to do that...

  2. #2
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Just define your operator ():

    Code:
    class SomeProcess
    {
    public:
    	template <typename Iterator>	
    	const SomeProcess& operator ()(Iterator pos, Iterator end)
    	{
    		// do something useful...
    		return *this;
    	}
    
    	template <typename Container>	
    	const SomeProcess& operator ()(Container& box)
    	{
    		return (*this)(box.begin(), box.end());
    	}
    };
    I chose to return a reference to the functor (standard approach), but you can return whatever you like - even void.

    Oh, and if you design a functor to operate on an object within a container, you could use it in functions like std::for_each, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functor errors
    By Dae in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2011, 05:10 AM
  2. delete functor
    By KIBO in forum C++ Programming
    Replies: 2
    Last Post: 05-08-2009, 08:25 AM
  3. Functor to allow access by callback
    By MarkZWEERS in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2008, 06:31 AM
  4. Help with default parameter value using ptr-to-functor
    By registering in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2004, 04:21 PM
  5. Map and Functor :: STL
    By kuphryn in forum C++ Programming
    Replies: 8
    Last Post: 11-26-2002, 12:09 PM