简体   繁体   English

使用专用功能的异构集合上的STL算法

[英]STL algorithms on heterogeneous collections by using specialized functions

I would like to use algorithm std::include to work with heterogeneous STL collections. 我想使用算法std::include来处理异构STL集合。 In my example I would like to check if a std::vector of integer is included in a std::map . 在我的示例中,我想检查std::map是否包含整数的std::vector

I would like to solve this problem by using a simple templated function; 我想通过使用一个简单的模板化函数来解决此问题; the reason for this is because I would like to use the C++ template argument deduction to let deduce when first argument of the comparer function is a std::pair vs an int and the other way around ( std::include behind the scenes calls Comp(a,b) and Comp(b,a) ). 原因是因为当比较器函数的第一个参数是std :: pair与int时,我想使用C ++模板参数推论来推断( std::include在幕后调用Comp (a,b)和Comp(b,a))。

Below my code I would like to run 在我的代码下面,我想运行

    typedef std::map<int,std::string> dict;
    typedef std::vector<int> vect;

    int data[]={1,2,3,4};
    vect l(data,data+4);
    dict h;
    h.insert(dict::value_type(0,"ciccio"));
    h.insert(dict::value_type(1,"ciccio"));                  
    std::includes( h.begin(),h.end()
        , l.begin(), l.end(), is_my_less );

I tried the following below but it doesn't compile and say partial specialization is not allowed but also unresolved overloaded function type that makes me think that I am doing something wrong with my function. 我在下面尝试了以下内容,但它没有编译,并说partial specialization is not allowed但也存在unresolved overloaded function type ,这使我觉得我的函数做错了。 Do you know is that is possible by using strictly a templated function? 您知道严格使用模板化功能可以实现吗?

    template<class T1,class T2,class T3>
    bool is_less_than_pair( const T1&a ,const T2& b ){
        return false;
    };

    template<class T1,class T>
    bool is_less_than_pair<
        T1
        , std::pair<T1,T>
        , T >( const T1&a, const std::pair<T1,T>& b ){
        return a<b.first;
    }

    template<class T1, class T>
    bool is_less_than_pair< 
        std::pair<T1,T>
        ,T1
        , T >( const std::pair<T1,T>& a, const T1& b ){
        return a.first<b;
    }

Now based on the fact that function templates cannot be partially specialized I tried function overloading like below but it didn't work out and gcc tells me unresolved overloaded function type again. 现在基于函数模板不能被部分专业化的事实,我尝试了如下所示的函数重载,但没有成功,gcc再次告诉我unresolved overloaded function type What is the best I can do? 我能做的最好的是什么?

      template<class T1,class T2>
      bool is_less_than_pair( const std::pair<T1,T2>& a ,const T1& b ){
        return a.first<b;
      };
      template<class T1,class T2>
      bool is_less_than_pair( const T1& a ,const std::pair<T1,T2>& b ){
        return b.first<a;
      };

If you're restricted to functions, no. 如果您只能使用函数,则不能。 You can't use an overloaded function, because it won't be able to select the proper overload when passing it to the STL algorithm. 您不能使用重载函数,因为当将其传递给STL算法时,它将无法选择适当的重载。 And you can't partially specialize a function template. 而且,您不能部分专门化功能模板。

It can be done with a function object, by providing all of the overloads. 通过提供所有重载,可以使用功能对象完成此操作。 In effect, you'll pass all of the overloads to the STL algorithm, and then it will choose the appropriate overload as it's called. 实际上,您会将所有重载传递给STL算法,然后它将在调用时选择适当的重载。 Note, due to the fact that the map's values are std::pair<const int, string>& , use of the same T1 in both sides of the mixed-mode operators will not work, since the vector's iterators are turning int&, where the map's are using a constant. 请注意,由于映射的值为std::pair<const int, string>& ,因此在混合模式运算符的两侧都不能使用相同的T1,因为矢量的迭代器正在将int&变为地图使用的是常量。

struct my_less_than
{
   template <typename T1>
   bool operator()(const T1& lhs, const T1& rhs) const
   {
      return lhs < rhs;
   }

   template <typename T1, typename T2, typename T3>
   bool operator()(const T1& lhs, const std::pair<T2, T3>& rhs) const
   {
      return lhs < rhs.first;
   }

   template <typename T1, typename T2, typename T3 >
   bool operator()(const std::pair<T1, T2>& lhs, const T3& rhs) const
   {
      return lhs.first < rhs;
   }

   template <typename T1, typename T2, typename T3>
   bool operator()(const std::pair<T1, T2>& lhs, const std::pair<T1, T3>& rhs) const
   {
      return lhs.first < rhs.first;
   }
};

Usage: 用法:

   std::includes( h.begin(),h.end()
        , l.begin(), l.end(), my_less_than() );

Edit: Example http://ideone.com/JFIoy 编辑:示例http://ideone.com/JFIoy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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