Search code examples
c++lambdafunction-pointers

function pointer with vector and class


In the purpose of my homework, I learned pointer to function and lambda function.
I create a class Rectangle that contain width and length and calculate area. One of the question is to create a class MyVector that is a derived class of stl vector and contain function called func that take in parameter a Boolean function and return true if at least one of the element in the vector is answer to the function otherwise, false will be returned.(I called this boolean function cmp)
In the main, I have to check if at least one rectangle have an area more that 50, if it is the case display all rectangle. I don't know how to use this pointer function very well, so can you help me understand this through my example

Rectangle class:

#pragma once
#include <iostream>
using namespace std;
class Rectangle
{
private:
    int width;
    int length;
public:
    Rectangle(int x, int y) : width(x), length(y) {};
    int area() { width* length; }
    friend ostream& operator<<(ostream& os, const Rectangle& r);
};
ostream& operator<<(ostream& os, const Rectangle& r)
{
    os << r.width << " " << r.length << endl;
    return os;
}

MyVector class:

#include <iostream>
#include <vector>
#include <algorithm>
#include "Rectangle.h"
using namespace std;


template <class T>
class MyVector : public vector<T>
{
public:
    bool func(bool(*cmp)); //This is a function I create, I don't know how to use cmp function to compare area
};
template <class T>
bool MyVector<T>::func(bool(*cmp))
{
    MyVector<T>::iterator it;
    for (it = MyVector.begin(), it < MyVector.end(), it++)
        if (func(*it))
            return true;
    return false;

}
#include <iostream>
#include "Rectangle.h"
#include "MyVector.h"
using namespace std;

int main()
{

        MyVector<Rectangle> Myvec;
        
        Myvec.push_back(Rectangle(2, 4));
        Myvec.push_back(Rectangle(4, 8));
        Myvec.push_back(Rectangle(8, 16));
        Myvec.push_back(Rectangle(16, 32));
        Myvec.push_back(Rectangle(32, 64));
        Myvec.push_back(Rectangle(64, 128));

        if (Myvec.func(/*Here I have to complete*/) 
                //If func function return true print all vector element
            );

    return 0;
}

Solution

  • You'll need to make a few changes to the implementation of func():

    template <class T>
    bool MyVector<T>::func(bool(*cmp)(const T&))
    {
        typename MyVector<T>::iterator it;
        for (it = this->begin(); it < this->end(); it++)
            if (cmp(*it))
                return true;
        return false;
    
    }
    

    The main differences between what you had and this are:

    • the function pointer coming in will accept a single parameter of type const T&
    • the iteration happens over this->begin() to this->end() (what you had there wouldn't have compiled)
    • the cmp(*it) is used instead of func(*it)

    then you can actually use it in the main function like:

    bool larger_than_50_area(const Rectangle& r) {
        return r.area() > 50;
    }
    
    int main()
    {
        MyVector<Rectangle> Myvec;
        ...
    
        if (Myvec.func(larger_than_50_area)) {
            ...
        }
        return 0;
    }
    

    Let me know if that helps!