|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 laocaiye 于 2025-3-24 22:04 编辑
下面这段代码编译或运行时怎么老是提示错误
#include <iostream>
#include <set>
using namespace std;
class MyCompare
{
bool operator()(const int &v1,const int &v2)
{
return v1 > v2;
}
};
void test4()
{
set<int, MyCompare>st;
st.insert(10);
st.insert(70);
st.insert(30);
st.insert(80);
st.insert(20);
for (set<int, MyCompare>::iterator it = st.begin(); it != st.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test4();
system("pause");
return 0;
}
- #include <iostream>
- #include <set>
- using namespace std;
- class MyCompare
- {
- public: // 这里需要添加public访问修饰符
- bool operator()(const int &v1,const int &v2)
- {
- return v1 > v2;
- }
- };
- void test4()
- {
- set<int, MyCompare>st;
- st.insert(10);
- st.insert(70);
- st.insert(30);
- st.insert(80);
- st.insert(20);
- for (set<int, MyCompare>::iterator it = st.begin(); it != st.end(); it++)
- {
- cout << *it << " ";
- }
- cout << endl;
- }
- int main()
- {
- test4();
- system("pause");
- return 0;
- }
复制代码
|
|