开发者

how to define a vector with a functor

hey, i implemented the following functor:

struct CompareCatId : public std::binary_function<Vehicle*, Vehicle*, bool>
{
    bool operator()(Vehicle* x, Vehicle* y) const
    {   
        if(x->GetVehicleType() > y->GetVehicleType())
            return true;
        else if (x->GetVehicleType() == y->GetVehicleType() && x->GetLicenseNumber() > y->GetLicenseNumber())
            return true;
        else
            return false;
    }
};

when i try to define a vector as the following i am getting alot of errors :

vector开发者_如何学Go<Vehicle*,CompareCatId>* m_vehiclesVector;

thanks in advance for your help.


vector does not take a functor, so you can't.

vector has two template parameters: the type of object to be stored and the allocator to be used (the allocator is optional; by default it will use std::allocator<T>).

The ordered associative containers (e.g., map and set) allow you to specify a comparison function because they are ordered containers: they have to keep their elements in some order.

If you want to keep the elements of the vector sorted you need to sort them yourself, either by inserting each new element into the correct position in the vector such that it always stays sorted or by sorting the vector after you have finished inserting elements. Alternatively, you can use one of the ordered associative containers, like set.


vector isn't a sorted container and as such doesn't accept a comparison type.

I think you're looking for std::set<Vehicle*,CompareCatId>* m_vehiclesVector;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜