0

Hey i am trying to transform a vectors of pointers to an object into another vector of pointers to a different object.

I have been given the following classes:

class Test;
class Test2 {
    public:
        Test2(Test*);
}

I am unfamiliar on how to create a functor. this is part of a sample exam for exams later this week i am confused on this question though. The following link explains the use of transform quite well but my problem is a little different. Any help would be great!

Reference: http://www.cplusplus.com/reference/algorithm/transform/

Damian Yerrick
  • 4,602
  • 2
  • 26
  • 64
George Veron
  • 35
  • 1
  • 6
  • [What have you tried?](http://www.whathaveyoutried.com) – Gnosophilon Jun 04 '12 at 15:41
  • Hmmm, homework question.... Have you read and tried to understand this? http://stackoverflow.com/questions/356950/c-functors-and-their-uses – KayEss Jun 04 '12 at 15:42
  • If you're unfamiliar with how to create a functor, why don't you get familiar with it (use a search engine, for example), retry your problem, and if you still fail to solve it because of a specific difficulty, ask on SO – Armen Tsirunyan Jun 04 '12 at 15:42
  • vector v1; vector v2; v1[0] = new Test(); v1[1] = new Test(); transform(v1.begin(),v1.end(), v2.begin(), 1); transform(v1.begin(), v1.end(), v2.begin(), v1.begin(),1); – George Veron Jun 04 '12 at 15:43
  • I'm having a hard time following the question. What is the source, `std::vector`? And the target, `std::vector`? – Chad Jun 04 '12 at 15:44
  • @Chad yep thats it. The question asks to write code to transform a vector of pointers to Test instances into a vector of pointers to Test2 instances – George Veron Jun 04 '12 at 15:46
  • p.s trying to fix my code comments not sure how to use code tags exactly – George Veron Jun 04 '12 at 15:46
  • So is a question-and-answer site, but there is no question in this post. @GeorgeVeron, do you have a question? – Robᵩ Jun 04 '12 at 15:58

2 Answers2

2

A functor is one which overloads operator(). So you can define a functor as:

struct Test2Generator
{
     //this will be called from std::transform
     Test2* operator()(Test1 *param) const
     {
          return new Test2(param);
     }
};

Then use it as:

std::vector<Test1*> v1;
//fill v1

std::vector<Test2*> v2(v1.size());

std::transform(v1.begin(), v1.end(), v2.begin(), Test2Generator());

In C++11, you don't need to define functor, instead you can use lambda as:

std::transform(v1.begin(), 
               v1.end(), 
               v2.begin(), 
               [](Test1 *param) { return new Test2(param); });
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 1
    Thanks for that ill test it out now and go through it – George Veron Jun 04 '12 at 15:57
  • 1
    I seem to have gotten it working i will check with my tutor tomorrow but really helpful and i understand it now :) – George Veron Jun 04 '12 at 16:07
  • Small nitpick: It might be better not to `resize` but only `reserve` and use a `std::back_inserter` iterator. `resize()` requires initialization of all the elements in the vector. This will not really have a noticeable impact, but there is no point on initializing the pointers to 0 upfront. – David Rodríguez - dribeas Jun 04 '12 at 16:30
  • @DavidRodríguez-dribeas: Since it will not really have a noticeable impact (as you said), I preferred typing less in this answer. I'm kinda lazy; that is also because I saw your answer does that job pretty well. So I kept my answer a little different. – Nawaz Jun 04 '12 at 16:41
2

The call to transform will be something similar to:

std::vector<Test*> src = ...
std::vector<Test2*> dst;
dst.reserve( src.size() );
std::transform( src.begin(), src.end(), std::back_inserter(dst), MyFunctor() );

Where MyFunctor() is a function object that implements Test2* operator()( Test* ):

struct MyFunctor {
   Test2* operator()( Test* ) const {
      // implementation goes here
   }
};
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489