我想使用算法std::include
来处理异构STL集合。在我的示例中,我想检查std::map
中是否包含整数的std::vector
。通过使用专用函数异构集合的STL算法
我想通过使用简单的模板函数来解决这个问题;原因是我想使用C++模板参数推导来推断比较器函数的第一个参数是std :: pair还是int,反之亦然(std::include
幕后调用Comp(a, b)和Comp(b,a))。
下面我的代码,我想运行
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);
我尝试下面的下面,但它并没有编译和说partial specialization is not allowed
也unresolved overloaded function type
这让我觉得我做错了什么与我的功能。 你知不知道是否可以严格使用模板函数?
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;
}
现在基于一个事实,即函数模板不能部分特我试图函数重载像下面,但它没有工作和gcc告诉我再次unresolved overloaded function type
。我能做的最好的是什么?
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;
};
不能partialy专门的函数模板。 – jrok
@jrok。非常感谢我添加了另一个潜在的解决方案,因为这个事实,但它不工作......你能帮忙吗? –