困惑试图写一个通用助手类 [英] Confused trying to write a generic helper class

查看:131
本文介绍了困惑试图写一个通用助手类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写,将工作与任何一种整数容器的辅助类。具体来说,我的课将查找基于某些标准容器值。为了与不同类型的容器的工作,我的课显然需要不上的容器迭代器的容器本身,而是运营。我很困惑如何正确地定义和使用这个类。

 模板<类ForwardIt> //我一定要模板整个班级?
    类MyLookup {
       上市:
       模板<类ForwardIt> //或可我只是模板的构造
       MyLookup(ForwardIt开始,ForwardIt年底,...)
    }
 

执行情况是这样的,只有类的构造函数需要得到开始/结束迭代器对。我的问题是:

  1. 请我一定要模板类或者可能我只是模板的构造?
  2. 什么是实例化类的正确方法?编译器不喜欢

    MyLookUp<的std ::矢量< int>的:迭代>查找(foo.begin(),foo.end(),...)

这个问题是取代了<一href="http://stackoverflow.com/questions/24919513/trouble-with-template-constructor-of-non-template-class">ANOTHER 之一

解决方案

有关1.模板只是构造是确定的,并有一个很好的副作用:你不会需要指定模板参数,因为它们会被检测到<。 / P>

有关2.如果您坚持使用模板化类,你在

一个小问题

  MyLookUp&LT;性病::矢量::迭代器&GT;查找(foo.begin(),foo.end(),...)
 

的std ::矢量不是一个类型,而是一个模板。你需要说例如。

  MyLookUp&LT;的std ::矢量&lt; INT&GT; ::迭代器&GT;查找(foo.begin(),foo.end(),...)
 

I am trying to write a helper class that would work with any kind of container of integers. Specifically, my class would lookup container values based on some criteria. In order to work with different types of containers, my class obviously needs to operate not on containers themselves but on container iterators. I am confused how to properly define and use this class.

   template<class ForwardIt>    // Do I have to template entire class?
    class MyLookup {
       public: 
       template<class ForwardIt>   // Or may I just template the constructor
       MyLookup(ForwardIt begin, ForwardIt end, ...)
    }

Implementation is such that only class constructor needs to get begin/end iterator pair. My questions are:

  1. Do I have to template entire class or may I template just the constructor?
  2. What is the correct way to instantiate the class? Compiler dislikes

    MyLookUp< std::vector< int>::iterator> lookup(foo.begin(), foo.end(), ...)

THIS QUESTION WAS SUPERSEDED BY ANOTHER ONE

解决方案

For 1. Templating just the constructor is ok, and have a nice side effect: you will not need to specify template parameters, because they will be detected.

For 2. If you stick with templating the class, you have a small problem in

MyLookUp< std::vector::iterator> lookup(foo.begin(), foo.end(), ...)

std::vector is not a type, but a template. You need to say e.g.

MyLookUp< std::vector<int>::iterator> lookup(foo.begin(), foo.end(), ...)

这篇关于困惑试图写一个通用助手类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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