Sort comparator function


F

Fab

Dear All,

I am quite experienced with C++ but here's something I can't recall and
forgive me if I just don't recognize the (possible) simplicity of my
question.

I have the following code

// Custom comparator for pair,
// compares only by the first field
struct compare_first
{
template <typename T>
bool operator() (const T& a, const T& b)
{
return a.first < b.first;
}
};

which is later used in a sort like this

std::sort(cellid_particleid.begin(), cellid_particleid.end(), compare_first());

The sort orders a list of particles according to their cell ID. The
std::pair<unsigned int, unsigned int> contains cellID and particleID,
respectively.

Since the operator() is templated in the comparator, why does my code
still compile? I assume the struct hides the templation, but is this
taken care of in std::sort then?

Thanks + best
Fab
 
Ad

Advertisements

A

Alf P. Steinbach

I am quite experienced with C++ but here's something I can't recall and
forgive me if I just don't recognize the (possible) simplicity of my
question.

I have the following code

// Custom comparator for pair,
// compares only by the first field
struct compare_first
{
template <typename T>
bool operator() (const T& a, const T& b)
{
return a.first < b.first;
}
};

which is later used in a sort like this

std::sort(cellid_particleid.begin(), cellid_particleid.end(), compare_first());

The sort orders a list of particles according to their cell ID. The
std::pair<unsigned int, unsigned int> contains cellID and particleID,
respectively.

Since the operator() is templated in the comparator, why does my code
still compile? I assume the struct hides the templation, but is this
taken care of in std::sort then?

From the information given it's impossible to say what makes the code
compile.

See the FAQ item titled "How do I post a question about code that
doesn't work correctly?", currently available at (among others) <url:
http://www.parashift.com/c++-faq/posting-code.html), in particular the
three first bullet points.

It's often a good idea to take a look at the FAQ.


Cheers & hth.,

- Alf
 
Ad

Advertisements

S

sg

Am 14.12.2013 12:05, schrieb Fab:
I have the following code

struct compare_first
{
template <typename T>
bool operator() (const T& a, const T& b)
{
return a.first < b.first;
}
};

which is later used in a sort like this

std::sort(cellid_particleid.begin(), cellid_particleid.end(), compare_first());

Since the operator() is templated in the comparator, why does my code
still compile?

Why not? -- Well, you probably should make the function call operator
const. But std::sort does not seem to mind.
I assume the struct hides the templation, but is this
taken care of in std::sort then?

std::sort has a named third parameter that refers to some object of your
type. Let's say, it's "comp". Then, std::sort uses that parameter like this

comp(a,b)

for some sequence elements a and b. What happens now is that the
compiler deduces the function call operator's template parameter T and
instantiates it accordingly. Done.

Anything still unclear?

Cheers!
sg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top