C ++ interesting define function

I reviewed the RDcode code. And I come across a specific function, and I do not understand. Can you help me with this code.

template <typename T>
   class argless {
   public:
     argless(const T& c) : container(c) {}; // i dont understant this line.
     bool operator() (unsigned int v1,unsigned int v2){
       return container[v1]<container[v2];
     }
     const T &container;
   };
+3
source share
6 answers

This is a constructor that references const Tand initializes a member variable containerwith it.

+8
source

It uses initialization syntax to store the container passed through the const reference in the element's variable container. It should use the initialization syntax because the container member variable is a reference (it must be initialized through a list of initializers).

+3
source
+3

, , .. memberOfT& operator[] (unsigned int).

, memberOfT bool operator<(const memberOfT&, const memberOfT&), operator(), , .

, , T . argless , , , argless, argless.

0

:

1) .

2) .

0

It is used so that the operator () can act as the <operator for the components of another container. You would use it to sort one container based on the contents of another, where the first contains indexes in the second. In this case, you will find out what a sorted container looks like without having to sort it - a useful thing if moving items around is expensive or impossible, such as strings.

0
source

Source: https://habr.com/ru/post/1769891/


All Articles