c++ set 比较函数
1#include<iostream>2#include<string>3#include<set>4#include<fstream>5usingnamespacest...
1 #include<iostream>
2 #include<string>
3 #include<set>
4 #include<fstream>
5 using namespace std;
6 class a
7 {
8 public:
9 bool operator()(int a,int b){return a>b;}
10 bool myprint(){cout<<"this is myprint function"<<endl;}
11 };
12
13 int main()
14 {
15 set<int,a> tmp;
16 tmp.insert(5);
17 tmp.insert(4);
18 set<int>::key_compare b=tmp.key_comp();
19 b.myprint;
20
21 }
编译的时候:g++ test1.cpp
test1.cpp: In function 'int main()':
test1.cpp:18: error: conversion from 'a' to non-scalar type 'std::less<int>' requested
test1.cpp:19: error: 'struct std::less<int>' has no member named 'myprint'
疑惑:
Template<class T, class Compare=Less<T>,class Alloc=allocator<T> > class set;
typedef Compare key_compare;
typedef Compare value_compare;
可是我在定义变量tmp的时候,已经将Compare类型设置为类a了。那么Compare,key_compare,value_compare不是都应该是类a的类型吗?。为什么从编译的错误提示上看,key_compare,value_compare还是Less类型呢? 展开
2 #include<string>
3 #include<set>
4 #include<fstream>
5 using namespace std;
6 class a
7 {
8 public:
9 bool operator()(int a,int b){return a>b;}
10 bool myprint(){cout<<"this is myprint function"<<endl;}
11 };
12
13 int main()
14 {
15 set<int,a> tmp;
16 tmp.insert(5);
17 tmp.insert(4);
18 set<int>::key_compare b=tmp.key_comp();
19 b.myprint;
20
21 }
编译的时候:g++ test1.cpp
test1.cpp: In function 'int main()':
test1.cpp:18: error: conversion from 'a' to non-scalar type 'std::less<int>' requested
test1.cpp:19: error: 'struct std::less<int>' has no member named 'myprint'
疑惑:
Template<class T, class Compare=Less<T>,class Alloc=allocator<T> > class set;
typedef Compare key_compare;
typedef Compare value_compare;
可是我在定义变量tmp的时候,已经将Compare类型设置为类a了。那么Compare,key_compare,value_compare不是都应该是类a的类型吗?。为什么从编译的错误提示上看,key_compare,value_compare还是Less类型呢? 展开
2个回答
展开全部
#include<iostream>
#include<string>
#include<set>
#include<fstream>
using namespace std;
class a {
public:
bool operator()(int a,int b) {
return a>b;
}
bool myprint() {
cout<<"this is myprint function"<<endl;
return true;
}
};
int main() {
a thea;
set<int,a> tmp(thea);// 这里
tmp.insert(5);
tmp.insert(4);
set<int,a>::key_compare b=tmp.key_comp(); // 这里要用完整的类型,要加a
b.myprint();
}
因为你没把比较运算子的实例传递给set的构造函数,光是用template参数指定类型是不行的,不可能让set自己去创建一个运算子的对象,因为如果运算子是函数指针类型则没办法创建,如果没有默认构造函数的类也没法创建。
展开全部
#include<iostream>
#include<string>
#include<set>
#include<fstream>
using namespace std;
class a
{
public:
bool operator()(int a,int b){return a>b;}
bool myprint(){cout<<"this is myprint function"<<endl;}
};
int main()
{
set<int,a> tmp;
tmp.insert(5);
tmp.insert(4);
set<int,a>::key_compare b=tmp.key_comp();
b.myprint();
}
#include<string>
#include<set>
#include<fstream>
using namespace std;
class a
{
public:
bool operator()(int a,int b){return a>b;}
bool myprint(){cout<<"this is myprint function"<<endl;}
};
int main()
{
set<int,a> tmp;
tmp.insert(5);
tmp.insert(4);
set<int,a>::key_compare b=tmp.key_comp();
b.myprint();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询