简体   繁体   中英

c++ interesting function define

i have examined code of RDcode. And i come across a defined function and i dont understand. Can you help me to 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;
   };

它是一个构造函数,它接受const T的引用,并用它初始化成员变量container

It uses initializer syntax to store the container passed in by const reference in the member variable container. It has to use initializer syntax, because the container member variable is a reference (it must be initialized via an initializer list).

这是一个使用初始化列表构造 函数

This is a class template for a functor that allows comparison of arbitrary elements in an indexable container, ie. one that implements memberOfT& operator[] (unsigned int) .

In addition memberOfT must supporting bool operator<(const memberOfT&, const memberOfT&) , otherwise the operator() that makes this a functor will not compile.

The line you are asking about simply makes an input container of class T available to other members of the class. Since this is held in argless as a reference, it's important that the source container used on the constructor of argless remain in scope for the duration of argless 's use.

It is the syntax for 2 types of stuff:

1) Initializing a const variable.

2) Calling The constructor's base class.

It is used to enable operator() to act like 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 into the second. In this case, you would gain the knowledge of what a sorted container looks like, without actually having to sort it - a useful thing if moving the elements around is expensive or impossible, such as strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM