C++ 关于模板的问题
我有以下代码C++ 关于模板的问题,c++,C++,我有以下代码 #include <iostream> #include <utility> using namespace std; namespace rel_ops{ template<class t>bool operator!=(const t& x, const t& y){ return !(x==y);} template <class t>bool operator>(const t&
#include <iostream>
#include <utility>
using namespace std;
namespace rel_ops{
template<class t>bool operator!=(const t& x, const t& y){ return !(x==y);}
template <class t>bool operator>(const t& x,const t& y){ return y<x;}
template <class t>bool operator <=(const t& x,const t& y){ return !(y<x);}
template <class t> bool operator>=(const t& x,t& y) { return ! (x<y);}
}
int main(){
int x,y;
cin>>x>>y;
return 0;
}
#包括
#包括
使用名称空间std;
名称空间相关操作{
templatebool操作符!=(常量t&x,常量t&y){return!(x==y);}
模板布尔运算符>(常量t&x,常量t&y){返回y>y;
返回0;
}
我有个问题
如何在主函数中实现它?如何在主函数中实现它的运算符这里是一个解释和一个很好的示例:
下面是一个解释和一个很好的例子: 您只需添加:
using namespace rel_ops;
请注意,std中已经定义了rel_ops。您不需要在代码中重新定义此命名空间及其内容。要使用std中已有的定义,只需使用:
#include <utility>
using namespace std::rel_ops;
#包括
使用名称空间std::rel_ops;
您只需添加:
using namespace rel_ops;
请注意,std中已经定义了rel_ops。您不需要在代码中重新定义此命名空间及其内容。要使用std中已有的定义,只需使用:
#include <utility>
using namespace std::rel_ops;
#包括
使用名称空间std::rel_ops;
您到底想做什么?使用此运算符将此模板与x和yY一起使用您误解了它们的用途。它们是用来帮助类定义所有比较运算符的。定义后,您只需像使用x
一样使用它们。您到底想做什么?使用此运算符将此模板与x和yY一起使用你误解了它们的用途。它们是用来帮助类定义所有比较运算符的。一旦定义了它们,你就可以像x
一样使用它们。