Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 大函子不';行不通_C++_Templates_Generics_Stl_Functor - Fatal编程技术网

C++ 大函子不';行不通

C++ 大函子不';行不通,c++,templates,generics,stl,functor,C++,Templates,Generics,Stl,Functor,这是代码 #include <iostream> #include <algorithm> #include <functional> using namespace std; int main() { int a = 1; int b=2; if(greater<int>(a,b)) cout<<"YES"; else cout<<"NO"; } #包括

这是代码

#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

int main()
{
    int a = 1;
    int b=2;
    if(greater<int>(a,b))
        cout<<"YES";
    else
        cout<<"NO";
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
INTA=1;
int b=2;
如果(大于(a,b))

不能用下面的方法写

if(greater<int>()(a,b))
if(更大的()(a,b))

要比较值,类std::greater使用其运算符函数

std::greater
的比较功能通过其
运算符()
或函数调用运算符完成

greater<int>(a,b)

它的工作方式如下:

int a = 1;
int b=2;
greater<int> g;
if(g(a,b))
cout<<"YES";
else
cout<<"NO";
inta=1;
int b=2;
大g;
如果(g(a,b))

cout
std::greater
是一个类模板。您需要首先创建一个实例,即
std::greater()(a,b)如果李察回答了你的问题,请考虑通过点击旁边的灰色复选标记来接受答案。这会让其他人知道你不再积极寻找一个改进的解决方案,并在堆栈溢出上为你赢得新的徽章。
int a = 1;
int b=2;
greater<int> g;
if(g(a,b))
cout<<"YES";
else
cout<<"NO";