C++ 错误 具有类型“const sort”的表达式会丢失一些 const-volatile 限定符以调用“bool sort::operator ()(int,int)” 如下:环境 vs2019 内容:set内置函数排序
C++ 错误 具有类型“const sort”的表达式会丢失一些 const-volatile 限定符以调用“bool sort::operator ()(int,int)” 如下:
环境 vs2019
内容:set内置函数排序
1 #include <iostream> 2 #include<set> 3 #include<string> 4 5 using namespace std; 6 7 8 class sort 9 { 10 public: 11 bool operator()(int v1, int v2) 12 { 13 return v1 > v2; 14 } 15 }; 16 17 18 void test01() 19 { 20 set<int>s1; 21 s1.insert(30); 22 s1.insert(10); 23 s1.insert(50); 24 s1.insert(20); 25 s1.insert(40); 26 27 for (set<int>::iterator it = s1.begin(); it != s1.end(); it++) 28 { 29 cout << *it << " "; 30 } 31 cout << endl; 32 33 set<int, sort>s2; 34 s2.insert(30); 35 s2.insert(10); 36 s2.insert(50); 37 s2.insert(20); 38 s2.insert(40); 39 40 for (set<int, sort>::iterator it = s2.begin(); it != s2.end(); it++) 41 { 42 cout << *it << " "; 43 } 44 cout << endl; 45 46 47 } 48 int main() 49 { 50 51 test01(); 52 53 system("pause"); 54 return 0; 55 }
//改正 在仿函数中加入const
#include <iostream> #include<set> #include<string> using namespace std; class sort { public: bool operator()(int v1, int v2) const { return v1 > v2; } }; void test01() { set<int>s1; s1.insert(30); s1.insert(10); s1.insert(50); s1.insert(20); s1.insert(40); for (set<int>::iterator it = s1.begin(); it != s1.end(); it++) { cout << *it << " "; } cout << endl; set<int, sort>s2; s2.insert(30); s2.insert(10); s2.insert(50); s2.insert(20); s2.insert(40); for (set<int, sort>::iterator it = s2.begin(); it != s2.end(); it++) { cout << *it << " "; } cout << endl; } int main() { test01(); system("pause"); return 0; }
本文来自博客园,作者:Bytezero!,转载请注明原文链接:https://www.cnblogs.com/Bytezero/p/15145954.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)