In the search for the longest line I saw the word компаратор , but since I do not understand much in programming, I do not know what it can be. Explain, please.

  • 6
    In Russian it will be a “comparator”, i.e. The device / function that performs the comparison. - ixSci
  • Compare in English - compare. Comparator, as it should - comparator; what compares. - arrowd
  • And more can you please? - Pollo

3 answers 3

Well, for example, how is the sorting performed?

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

In this case, the sorted elements are simply compared using the < operator. So he is the default comparator. But if you want some very tricky sorting, then it is done like this.

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

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

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

 sort(v.begin(),v.end(), // Вот это лямбда-выражение и есть компаратор: [](const string& a, const string& b){ return a.length() < b.length(); ); 

will sort the strings by length.

The same can be written with the usual function:

 // Здесь компаратор - функция comp: bool comp(const string& a, const string& b) { return a.length() < b.length(); } sort(v.begin(),v.end(), comp); 

    A comparator is a special function that can compare two objects and decide whether it is more-less-equal.

    Why do you need it?

    Imagine writing a sort of an array of objects. It will be necessary for each new type of object to write its own sorting (after all, the sorting function should be able to compare two objects). Therefore, it was decided that a function that can make a comparison can be transferred to sorting, and the sorting function becomes universal.

    Immediately there is an additional opportunity - you can change the sorting function, you can search by array, divide it into parts.

    Comparators themselves are of two main types.

    The first type is a comparator, which returns a bool. For example, std :: less . This method has one big problem. If a> b and b> a, then some functions with ++ consider that a == b. But it's not always the case.

    The second type is a comparator that returns -1, 0 and +1 (or the whole range of integers in general). The point is simple - 0 is equal, -1 is the first is greater, +1 is the second is more. An example is strcpm . Only here they have gone even further - if the number is not zero, then it doesn’t just show the position of the first different character. Conveniently.

    Now Sutter invented (or rather, peeped in Perl) the spacecraft operator ( <=> ). This operator works according to the second scheme and solves a bunch of problems of the first type comparator. Details in the offer .

      The word comparator comes from the English "compare", that is, "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 it is possible to compare strings directly, this is not always convenient. For example, if we compare the length of the lines, it will look like this:

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

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