模板,类比较 [英] template, class Compare

查看:39
本文介绍了模板,类比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是c ++的新手,但是在其他语言(如Java)方面有一些经验.我在链接 [

Hi, I am new to c++ but have some experience in other langues like Java. I saw one example at link[link] where they create a priority queue who have these parameters:


template < class T, class Container = vector<T>,
           class Compare = less<typename Container::value_type> >
class priority_queue;



他们说:
比较类:这样一个类,表达式comp(a,b),其中comp是此类的对象,而a和b是容器的元素."

到目前为止,我仍然很了解,但是当他们后来创建比较器时,它们没有继承"Compare"类,那么c ++如何知道运算符存在?这是某些运营商的特殊规定吗?


这是我正在谈论的代码:
(我删除了一些代码以使其更加清晰)
(来源
链接 [



They say:
"Comparison class: A class such that the expression comp(a,b), where comp is an object of this class and a and b are elements of the container."

So long I still understand, but when they later create a comparator they don''t inherit the class "Compare" so how does c++ know that the operator exist? Is this some special rule with some operators?


Here is the code I am talking about:
(I removed some code to make it more clear)
(source link[link])

class mycomparison // not something like : Compare  ???
{
public:
  bool operator() (const int& lhs, const int&rhs) const
  {
    return (lhs > rhs);
  }
};

int main ()
{
  int myints[]= {10,60,50,20};

  // using mycomparison:
  priority_queue< int, vector<int>, mycomparison > fourth;
  
  return 0;
}



预先感谢WaZoX.



Thanks in advance, WaZoX.

推荐答案

您遇到的问题似乎是您习惯于将成员函数与对象进行时间绑定.实际调用的成员函数由运行时的对象类确定.使用模板,您可以在编译时根据对象的类选择要调用的函数.编译器知道运算符存在,因为在为模板类生成代码时,它会检查运算符是否存在于给定的类对象中.

如果比较类具有priority_queue期望的语义成员函数,则您无需派生任何内容.只要提供给优先级队列的任何内容都可以应用函数调用语法,就可以在创建看起来像优先级队列的对象时使用它.这是一种编译时鸭子输入的形式-提供您指定的比较事物"可以具有您正在创建的类的操作,需要编译器为其创建代码.

实际上,使用该模板,您不必使用类.您还可以提供函数名称.

希望这会有所帮助,如果没有的话,请问一下!

干杯,

Ash
The problem you''re having seems to be that you''re used to run time binding of member functions to objects. The actual member function called is determined by the class of object at runtime. With templates you can choose the function to be called based on the class of the object at compile time. The compiler knows the operator exists because when it generates code for the template class it checks the operator exists on objects of the class it''s given.

Provided the comparison class has the semantics member functions the priority_queue is expecting then you don''t need to derive anything. Provided whatever is supplied to the priority queue can have function call syntax applied to it you can use it when you create objects that look a bit like priority queues. It''s a form of compile time duck typing - provided the comparison "thing" you specify can have the operations the class you''re creating needs the compiler can create the code for it.

In fact with that template you don''t have to use a class. You can also provide a function name.

Hope this helps, if not then ask away!

Cheers,

Ash


在另一种表述中,Compare 不是类"(与它们相关的数据和功能的集合),而是模板参数".它是在编译时被您指定的参数(在您的情况下为mycomparison)替换的东西.

因此,它们的内部代码在源代码中可能看起来像这样:
In other formulation, Compare is not a "class" (aggregate of data and functions related to them) but a "template parameter". It is something that at compile time is replaced with the parameter you specify (mycomparison, in your case).

So their internal code that in source may looks like this:
...
Comapre comp;
if(comp(a,b))
{ ... }
...



模板实例化后如下所示:



after the template instantiation becomes like this:

...
mycomparison comp;
if(comp(a,b))
{ ... }
...



由于所有表达式都有定义,因此可以完美地转换为机器代码.



that is perfectly translatable into machine code, since all the expression have a definition.


这篇关于模板,类比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆