• 以结构体为键值时的map报错


    struct ch{ //选课 
    	int st_id; // 学号 
    	int sb_id; // 课程号 
    };
    map<ch, int> cho;
    
    bool choose::check(ch f)
    {
    	int x = cho[f];
    	return x != 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    也不知道为什么,总之好像只要直接用一个结构体的变量作为键值而不是手写的{a, b}这个样子的,就会报一堆奇怪的错误

    52		D:\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\x86_64-w64-mingw32\bits\stdc++.h	                 from D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/x86_64-w64-mingw32/bits/stdc++.h
    2		C:\Users\30504\Desktop\新建文件夹 (2)\main.cpp	                 from main.cpp
    D:\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\bits\stl_function.h	In instantiation of 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = ch]':
    498	31	D:\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\bits\stl_map.h	required from 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = ch; _Tp = int; _Compare = std::less<ch>; _Alloc = std::allocator<std::pair<const ch, int> >; 
    std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = int; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = ch]'
    371	20	D:\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\bits\stl_function.h	[Error] no match for 'operator<' (operand types are 'const ch' and 'const ch')
    371	20	D:\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\bits\stl_function.h	[Note] candidates are:
    371	20	D:\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\bits\stl_function.h	[Note] candidates are:
    58	0	D:\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\regex	In file included from D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/regex
    
    108		D:\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\x86_64-w64-mingw32\bits\stdc++.h	                 from D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/x86_64-w64-mingw32/bits/stdc++.h
    1428	5	D:\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\bits\regex.h	[Note] template<class _Bi_iter> bool std::operator<(const std::sub_match<_BiIter>&, const typename std::iterator_traits<_Iter>::value_type&)
    
    1428	5	D:\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\bits\regex.h	[Note] template argument deduction/substitution failed:
    48	0	D:\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\string	In file included from D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/string
    
    ......
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    然后我查了一下原因,必须得加个排序才行

    如下

    struct ch{ //选课 
    	int st_id; // 学号 
    	int sb_id; // 课程号 
    	bool operator < (const ch &a)const
        {
            if (st_id == a.st_id)   return sb_id < a.sb_id;
            else return st_id < a.st_id;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    这样就好了

    参考:https://www.cnblogs.com/ZhaoxiCheung/p/6790718.html

  • 相关阅读:
    暑假总结
    内存取证系列5
    史上最全面试题版!看完吊打面试官!七夕来袭!是时候展现专属于程序员的浪漫了 10万字+
    从月薪8k到月薪30k,自动化测试究竟该怎样学...
    孩子看书用白光还是暖白光?最适合写作业台灯推荐
    LetCode刷题[简单题](5)按摩师,迭代出最优解(卡尔曼滤波也是类似迭代)
    多分类-手写识别体
    运营-20.产品社区化和内容化
    Java开发学习(三十二)----Maven多环境配置切换与跳过测试的三种方式
    物流科技杂志物流科技杂志社物流科技编辑部2022年第11期目录
  • 原文地址:https://blog.csdn.net/qq_34682765/article/details/133660447