views:

163

answers:

4

Hi, I have a hw question which confuses me on what i have to do. The question is below:

  1. The idea is to design a generic function called Modify_If that will take an input x (passed by reference), and two functors f1 and f2. The function Modify_If will use functor f1 to determine whether x obeys a certain condition. If it does, Modify_if will change the value of x, by applying functor f2 to it.

The prototype for Modify_If is as follows:

template <class C, class Tester, class Transform>
void Modify_If(C & a, Tester f, Transform g)

I have to write the Modify_If function but I have no idea where to start so if anyone can help me out I would appreciate it.

+1  A: 

From the description it sounds like all you're supposed to do is to execute g(a) if f(a) returns true. That would look like this:

if(f(a)) {
  g(a);
}
sepp2k
+5  A: 
template <class C, class Tester, class Transform>
void Modify_If(C& a, Tester f1, Transform f2) {
    if (f1(a)) // Apply f1 to a - Check whether result is true
        a = f2(a); // Transform with f2; save
}
Dario
Did it really not occur to you that the person you helped getting this done without thinking themselves at all might one day become your co-worker and will make you ask how that guy ever managed to get a degree? -1
sbi
+1, questioner probably just needed to be shown that template code really is that simple.
Steve Jessop
@sbi: Well, tell him not to ask and not me not to answer!
Dario
It's somewhat assumed by the 'homework' tag on the question. See http://meta.stackoverflow.com/questions/10811/homework-on-stackoverflow
fbrereto
I had no idea that i wasn't allowed to ask a hw question here.
Zeeshan Raja
You are allowed to ask homework questions here. Some users don't like it, though, because they're not interested in answering homework questions. Also it's best to tag them, since some people will give a different kind of answer to homework questions (usually to help you find an answer, rather than just give you the code).
Steve Jessop
@onebyone: Than he should have been told to work with fixed types first, and templatize later - the way we all learned to deal with templates.
sbi
@fbrereto: That tag was added by me. I would assume people to think before they answer a question. Given the question, it wasn't hard to see it's a homework question.
sbi
@Zeeshan: I myself would encourage homework questions. (I had been teaching C++ for years.) It's just that I think homework questions should help you finding the solution yourself by thinking about the answers, broaden your horizon and, thus, become a better programmer. But I don't think at all that it is useful to give paste-able answers to such questions.
sbi
I like to provide code answers to everyone, because that means that in most cases they just copy the cody without thinking meaning less competition in workplace later on :)
stefanB
+1  A: 

In both functors Tester and Transform, all what you have to do is to create a class for each, and overload the () operator. Then write the body of the functor as if it were a function. This is a simple functor skeleton:

class Functor
{
public:
   template <class Type>
   void operator()(Type& value) // it doesn't have to be void.
   {
     // functor body, as if it were a function.
   }
};

Then, you can pass the functor as a parameter, by creating an object of it, or easily by creating it on the fly:

template <class Func>
void fun(Func f); // a function hat accepts a functor.
Functor f;
fun(f);
fun(Functor()); // creating the functor on the fly.
AraK
+1  A: 

Function prototype could look as follows:

template<class ForwardIterator, class Predicate, class Type>
void transform_if(
      ForwardIterator First, 
      ForwardIterator Last,
      Predicate Pred, 
      UnaryFunction Func
   );

In its implementation you should iterate through all set of elements and apply Func to all elements that will return true for Pred(*element_iterator).

Kirill V. Lyadvinsky