There is an assumption that it is possible to turn with sort(v.begin(), v.end(), cmp) , but I don’t understand how the third parameter is involved / what the third parameter means (Google also did not clarify)

    3 answers 3

    Assuming that you are interested in vector<pair<int,pair<int,int>>> , cmp might look something like this, for example:

     bool cmp(const pair<int,pair<int,int>>& p1, const pair<int,pair<int,int>>& p2) { return p1.second.second < p2.second.second; } 

      This is a link to a function of the form bool foo(const T& a, const T& b) , where T is the type of the elements of the sorted vector.

      The task of this function is to check whether a and b ordered in ascending order. In other words, does the value of a exceed that of b (in mathematical terms, whether the condition a < b is met).

      • Not necessarily a function, it is quite possible a functor (for example, lambda). And the signature should be bool (const T& a, const T& b) like. - VladD
      • I corrected the types of arguments (thanks for the remark), but is it worth it to load a novice with such concepts as “functor” and “lambda-function” (the latter, besides, is absent in the standards of the 98th and 2003th years)? - ߊߚߤߘ
      • one
        Well, at least show the opportunity to write a lambda as an advanced use? I would generally write “any garbage that can be called like this: const T& l; const T& r; bool v = fignya(l, r); const T& l; const T& r; bool v = fignya(l, r); ". Although this is probably too much. (But in the spirit of C ++.) - VladD
      • And who would explain to me how such a type is possible - vector<int,pair<int,int>> ? - Harry
      • @Harry, there is some typo. Well, the vector cannot accept the second template argument pair, only the memory management class for the elements. Probably meant by all the same vector<pair<int, pair<int, int> > > . - ߊߚߤߘ

      cmp is any functional object. It can be a lambda, a pointer to a function or an object of a class that has operator() defined. This function object must accept two objects from the list to be sorted, and return bool . The object must implement a strict weak ordering operation. This is how it will look like in your example:

       #include <iostream> #include <vector> #include <algorithm> typedef std::pair<int, std::pair<int, int>> IntIntInt; struct Compare{ inline bool operator()(const IntIntInt &x, const IntIntInt &y) const{ return x.second.second < y.second.second; } }; int main(){ std::vector<IntIntInt> v; v.push_back(std::make_pair(5, std::make_pair(3, 4))); v.push_back(std::make_pair(1, std::make_pair(9, 0))); v.push_back(std::make_pair(8, std::make_pair(6, 7))); v.push_back(std::make_pair(4, std::make_pair(2, 3))); v.push_back(std::make_pair(3, std::make_pair(1, 2))); std::sort(v.begin(), v.end(), Compare()); } 

      PS:

      Google also did not clarify

      Do not think for advertising. There is one wonderful site where you can peep the description of all algorithms, their arguments, guarantees of exceptions, complexity and examples of use.