dimanche 2 avril 2017

Comparing function of binary_search

I am trying to run binary_search on vector of custom objects.

struct T{
    string name;
    T(string n):name(n){};
    bool operator < (   T * n  ) const {
        return name < n ->  name;
    }
    bool operator == ( T * n ) const {
      return name == n -> name;
    }
};

    vector<T *> t;
    t.push_back(new T("one"));
    t.push_back(new T("two"));
    t.push_back(new T("three"));

bool has_3 = binary_search( t.begin(), t.end(), new T("two") ) ;
    if( has_3 ){
        cout <<"Its there" << endl;
    }

The comparation function should be just fine yet when i run the code has_3 equals to 0 = the element isnt present in vector. Is this problem caused by my overloading of < ? I see no reason why this shouldnt find the value. Considering the order of insertion into vector it should be sorted

Thanks for help.

Aucun commentaire:

Enregistrer un commentaire