Home c++ What is a comparator?

What is a comparator?

Author

Date

Category

In the question Finding the longest string I saw the word comparator , but since I don’t understand much in programming, then I don’t know what it might be. Please explain.


Answer 1, authority 100%

Well, for example, how is sorting done?

sort (v.begin (), v.end ());

The elements to be sorted are compared simply using the & lt; operator. This is the default comparator. But if you want some very tricky sorting, then this is done like this

sort (v.begin (), v.end (), comp);

where comp is the comparator – i.e. function, function object, lambda expression – a word that can be passed two elements for comparison.

For example, by default sorting strings will compare their contents.
And sorting

sort (v.begin (), v.end (),
  // This lambda expression is the comparator:
  [] (const string & amp; a, const string & amp; b) {return a.length () & lt; b.length ();
);

will sort lines by length.

The same can be written with a regular function:

// Here the comparator is the comp function:
bool comp (const string & amp; a, const string & amp; b)
{
  return a.length () & lt; b.length ();
}
sort (v.begin (), v.end (), comp);

Answer 2, authority 42%

A comparator is a special function that can compare two objects and decide if greater-less-than-equal.

Why do you need it?

Imagine you want to write sorting an array of objects. It will be necessary to write its own sort for each new type of objects (after all, the sorting function must be able to compare two objects). Therefore, we decided that we could pass a function to sorting that can do comparison, and the sorting function becomes universal.

An additional opportunity immediately appears – you can change sorting functions, you can search by an array, split into parts.

Comparators themselves are of two main types.

The first type is a comparator that returns bool. For example, std :: less . This method has one huge problem. If a & gt; b and b & gt; a, then some C++ functions think a == b. But this is not always the case.

The second type is a comparator that returns -1, 0 and +1 (or generally the entire range of integers). The essence is simple – 0 – equal, -1 – the first is greater, +1 – the second is greater. An example is strcpm . Only here we went even further – if the number is not zero, then it does not just show the position of the first different character. Convenient.

Now Sutter invented (or rather, spied on in Perl) a spaceship operator (& lt ; = & gt; ). This operator works according to the second scheme and solves a bunch of problems of the first type of comparator. Details in the offer .


Answer 3

The word comparator comes from the English “compare”, that is, “to compare”. It can be both electronic circuits and programs, code fragments, functions.
Since we are talking about C++, in this case it will be code. In C++, although you can compare strings directly, it is not always convenient. For example, if we compare the length of the strings, it will look like this:

string a, b;
getline (cin, a);
getline (cin, b);
if (a.length () == b.length ())
{
  cout & lt; & lt; "YES";
}

This is not very convenient. For this, a comparator is needed – with one function to compare the required string parameters. Very handy for sorting!
Comparators are available in the standard C++ libraries, or you can write your own.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions