C++ Visual Studio 2008中的错误<;算法>;?includes algo似乎无意中交换了第3795行上迭代器的顺序
我试图通过比较贴图的键和集合的值,得到C++ Visual Studio 2008中的错误<;算法>;?includes algo似乎无意中交换了第3795行上迭代器的顺序,c++,visual-c++,stl,stl-algorithm,C++,Visual C++,Stl,Stl Algorithm,我试图通过比较贴图的键和集合的值,得到includes算法来处理集合和贴图。当然,需要回避的问题是,map::value_type是pair,而set::value_type是V,因此,默认的包含谓词,这两个参数都需要相同的值类型 因此,我编写下面的谓词类来解决这个问题 template <class P,class K> struct mapKey_set_less : public std::binary_function<P,K,bool> { inline
includes
算法来处理集合
和贴图
。当然,需要回避的问题是,map::value_type
是pair
,而set::value_type
是V
,因此,默认的包含谓词
,这两个参数都需要相同的值类型
因此,我编写下面的谓词类来解决这个问题
template <class P,class K>
struct mapKey_set_less : public std::binary_function<P,K,bool>
{
inline bool operator() (const P& x, const K& y) const {return x.first < y ;};
};
template <class K,class P>
struct set_mapKey_less : public std::binary_function<K,P,bool>
{
inline bool operator() (const K& x, const P& y) const {return x < y.first ;};
};
带有无法将参数1从const V
转换为const std::pair
这似乎是由文件的第3795行引起的
if (_DEBUG_LT_PRED(_Pred, *_First2, *_First1))
其中第一个和第二个参数似乎已交换
在版本配置中编译会在第3795行出现相同的错误,因此Visual Studio实现似乎希望您仔细阅读此代码,除非您修改任何开关(如果有)以删除此“\u DEBUG\u LT\u PRED”宏
我的问题是:
这是VisualStudio中的一个bug,还是我犯了一个错误?在过去的十年中,我还没有做过任何C++,上周我不得不在一些紧凑的循环中加速,所以需要我能得到的所有帮助。
谢谢大家
很好
我没有考虑算法的要求。有道理,它需要检查双方的“小于”
不过,要编译它并没有什么乐趣
用我以前的代码
template <class P,class K>
struct mapKey_set_cmp //: public std::binary_function<P,K,bool>
{
inline bool operator() (const P& x, const K& y) const {return x.first < y ;};
};
编译器吐出
Error 1 error C2664: 'bool mapKey_set_less<PAIR,KEY>::operator ()(const KEY &,const PAIR &) const' : cannot convert parameter 1 from 'std::pair<_Ty1,_Ty2>' to 'const std::string &' c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility 346
Error 2 error C2664: 'bool mapKey_set_less<PAIR,KEY>::operator ()(const KEY &,const PAIR &) const' : cannot convert parameter 1 from 'std::pair<_Ty1,_Ty2>' to 'const std::string &' c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility 348
Error 3 error C2664: 'bool mapKey_set_less<PAIR,KEY>::operator ()(const KEY &,const PAIR &) const' : cannot convert parameter 2 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'const std::pair<_Ty1,_Ty2> &' c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility 346
Error 4 error C2664: 'bool mapKey_set_less<PAIR,KEY>::operator ()(const KEY &,const PAIR &) const' : cannot convert parameter 2 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'const std::pair<_Ty1,_Ty2> &' c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility 348
错误1错误C2664:'bool-mapKey\u-set\u-less::operator()(const-KEY&,const-PAIR&)const':无法将参数1从'std::PAIR'转换为'const-std::string&'c:\program files(x86)\microsoft visual studio 9.0\vc\include\xutility 346
错误2错误C2664:“bool-mapKey\u-set\u-less::operator()(const-KEY&,const-PAIR&)const”:无法将参数1从“std::PAIR”转换为“const-std::string&”c:\program files(x86)\microsoft visual studio 9.0\vc\include\xutility 348
错误3错误C2664:“bool-mapKey\u-set\u-less::operator()(const-KEY&,const-PAIR&)const”:无法将参数2从“std::basic\u string”转换为“const-std::PAIR&”c:\program files(x86)\microsoft visual studio 9.0\vc\include\xutility 346
错误4错误C2664:“bool-mapKey\u-set\u-less::operator()(const-KEY&,const-PAIR&)const”:无法将参数2从“std::basic\u string”转换为“const-std::PAIR&”c:\program files(x86)\microsoft visual studio 9.0\vc\include\xutility 348
仅看第一个错误,它似乎想要使用操作符()(KEY,PAIR)成对传递KEY。为什么它会忽略可用的运算符()(对,键)???
其他错误都是相同的,编译器“似乎”忽略了一个非常好的重载运算符
template <class P,class K>
struct mapKey_set_cmp //: public std::binary_function<P,K,bool>
{
inline bool operator() (const P& x, const K& y) const {return x.first < y ;};
inline bool operator() (const K& x, const P& y) const {return x < y.first ;};
};
感谢大家的帮助标准中没有要求第一个范围内的对象只能作为比较函数的左手参数传递,也没有要求第二个范围内的对象只能作为右手参数传递。因此,VisualStudio所做的是完全有效的,而且经常是必要的(见下文) 您可以编写一个处理这两种情况的函子:
template <class K,class P>
struct set_mapKey_less : public std::binary_function<K,P,bool>
{
inline bool operator() (const K& x, const P& y) const {return x < y.first;};
inline bool operator() (const P& y, const K& x) const {return y.first < x; }
};
...
std::map<int,int> m;
std::set<int> s;
std::includes(m.begin(), m.end(), s.begin(), s.end(),
set_mapKey_less<int,std::pair<int const,int> >());
如果只有一个小于运算符,在左边取一对值,在右边取一个整数,那么如何测试X是否包含Y
Is (3000,'a').first < 1000? No.
Is (3000,'a').first < 3000? No.
Is (3000,'a').first < 5000? Yes.
现在我们知道
3000
不小于(3000,'a')。第一个,和(3000,'a')。第一个不小于3000
,因此它们必须相等。@RobertHarvey你真的刚刚迁移了这个吗?请显示你调用std::includes
的代码。小提示:在“>>之间留出一个空格-这是一个C++11问题很好,我没有考虑算法的要求。有道理,它需要检查双方的“小于”。不过,要编译它并没有什么乐趣。我已经在原始问题中添加了细节。另一件事是,如果我们要以两种方式重载运算符(),那么从二进制函数继承可能会变得混乱,因为这规定P是第一个参数,K是第二个参数。我不确定这有多大区别,因为我相信这个类的定义只是一些typedef,但是如果stl正在使用这些typedef,我想它可能会混淆。无论如何,我已经把遗产拿走了。这对我得到的错误没有明显的影响。有人有机会看到我得到的编译错误吗?@OldSchool:当你发布它们时,我已经看过了。但是如果我看不到你的代码,我就无法诊断它,这就是为什么我在你的问题下面做了一个你从未回答过的评论。
template <class PAIR,class KEY>
struct mapKey_set_less : public std::binary_function<PAIR,KEY,bool>
{
inline bool operator() (const PAIR& x, const KEY& y) const {return x.first < y ;};
inline bool operator() (const KEY& x, const PAIR& y) const {return x < y.first ;};
};
int _tmain(int argc, _TCHAR* argv[])
{
map<string,double> theMap;
theMap["arse"] = 1;
set<string> theSet;
theSet.insert("arse");
typedef map<string,double>::iterator MI;
MI mi(theMap.begin()), miend(theMap.end());
typedef set<string>::iterator SI;
SI si(theSet.begin()), siend(theSet.end());
typedef mapKey_set_less< pair<string,double>,string> cmp;
if (includes(mi,miend,si,siend,cmp()))
{}
}
Error 1 error C2664: 'bool mapKey_set_less<PAIR,KEY>::operator ()(const KEY &,const PAIR &) const' : cannot convert parameter 1 from 'std::pair<_Ty1,_Ty2>' to 'const std::string &' c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility 346
Error 2 error C2664: 'bool mapKey_set_less<PAIR,KEY>::operator ()(const KEY &,const PAIR &) const' : cannot convert parameter 1 from 'std::pair<_Ty1,_Ty2>' to 'const std::string &' c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility 348
Error 3 error C2664: 'bool mapKey_set_less<PAIR,KEY>::operator ()(const KEY &,const PAIR &) const' : cannot convert parameter 2 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'const std::pair<_Ty1,_Ty2> &' c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility 346
Error 4 error C2664: 'bool mapKey_set_less<PAIR,KEY>::operator ()(const KEY &,const PAIR &) const' : cannot convert parameter 2 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'const std::pair<_Ty1,_Ty2> &' c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility 348
template <class K,class P>
struct set_mapKey_less : public std::binary_function<K,P,bool>
{
inline bool operator() (const K& x, const P& y) const {return x < y.first;};
inline bool operator() (const P& y, const K& x) const {return y.first < x; }
};
...
std::map<int,int> m;
std::set<int> s;
std::includes(m.begin(), m.end(), s.begin(), s.end(),
set_mapKey_less<int,std::pair<int const,int> >());
set X = { 1000, 3000, 5000, 7000, 9000 }
map Y = { (3000,'a') }
Is (3000,'a').first < 1000? No.
Is (3000,'a').first < 3000? No.
Is (3000,'a').first < 5000? Yes.
Is 3000 < (3000,'a').first? No.