Reputation: 551
In my main
class I've got a vector of pointer (vector<Corsa*> listaCorse
)
I want insert my Corsa*
object inside my ordered vector
So I create an iterator, and I pass him listaCorse.begin(), listaCorse.end(), myPointerToTheObject, &sortFunction)
Resolved: I don't know why, but I just included algorithm and vector, and now it compiles.
I really don't know why, because I included them inside the myhugeclass.h file, but it works, and that's ok
Thank you everyone :)
Problem: sortFunction is a static function inside my 'Corsa' class. And it is overloaded.
The two prototypers are:
bool Corsa::cmpCorse(Corsa *a, Corsa *b)
bool Corsa::cmpCorse(Corsa &a, Corsa &b)
Obviously I want to pass to him the first one, because mine is a vector of pointer
But my compiler doesn't like my code so much, so it says me 'unresolved overload function type'
Can anyone help me? :)
Thank you so much
This is the snippet of code:
// corsa.h
class Corsa{
...
static int cmpCorsa(const Corsa& a, const Corsa& b);
static int cmpCorsa(const Corsa *a, const Corsa *b);
...
}
// myhugeclass.cpp
int MyHugeClass::addCorsa(Corsa *a){
vector<Corsa*>::iterator low = lower_bound(listaCorse.begin(), listaCorse.end(), c, &Corsa::cmpCorsa);
listaCorse.insert(low, c);
return 0;
}
Thank you :)
EDIT: I made a big mistake, but not related tthis error. I passed the wrong function, I should pass static bool Corsa::sortCorsa(Corsa *a, Corsa *b)
Also this function is overloaded
As chris said (thank you chris), I try to do this:
int MyHugeClass::addCorsa(Corsa *c){
typedef bool(*Func)(const Corsa*, const Corsa*);
vector<Corsa*>::iterator low = lower_bound(listaCorse.begin(), listaCorse.end(), c, static_cast<Func>(&Corsa::sortCorsa));
listaCorse.insert(low, c);
return 0;
}
Now the error changes in [Error] no matching function for call to 'lower_bound(std::vector<Corsa*>::iterator, std::vector<Corsa*>::iterator, Corsa*&, bool (*)(const Corsa*, const Corsa*))'
Upvotes: 2
Views: 569
Reputation: 299730
This is an answer, and then maybe not...
When using sorted vectors (or deques) I have often found that using std::lower_bound
was not as practical as using std::partial_sort
:
// assume vec is sorted
size_t const currentSize = vec.size();
vec.push_back(...); // like usual
// or
std::copy(input.begin(), input.end(), std::back_inserter(vec));
std::partial_sort(vec.begin(), vec.begin() + currentSize, vec.end() /*, comparator*/);
It's as efficient as std::lower_bound
on a single insertion, and has the potential to be more efficient when multiple insertions take place.
Note: a call to std::sort
also works, and the implementation is usually optimized to take advantage of sorted runs, but it still seems wasteful.
Upvotes: 0
Reputation: 61900
You can cast it to the right type:
typedef bool(*Func)(const Corsa*, const Corsa*);
vector<Corsa*>::iterator low = lower_bound(..., static_cast<Func>(&Corsa::sortCorsa));
Upvotes: 3
Reputation: 35440
Here is a full example that compiles cleanly that uses std::sort. Note the typedef and the cast
#include <algorithm>
#include <vector>
struct Corsa
{
static bool cmpCorsa(const Corsa& a, const Corsa& b);
static bool cmpCorsa(const Corsa *a, const Corsa *b);
};
typedef bool (*fn)(const Corsa*, const Corsa*);
int main()
{
std::vector<Corsa*> C;
std::sort(C.begin(), C.end(), static_cast<fn>(&Corsa::cmpCorsa));
}
Upvotes: 1