• 以结构体为键值时的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

  • 相关阅读:
    JVM(十九) -- 字节码与类的加载(四) -- 再谈类的加载器
    C#中的for和foreach的探究与学习
    2022年最新湖北水利水电施工安全员考试题库及答案
    入门四认识HTML
    [西湖论剑 2022]web部分题解(更新中ing)
    竞赛 题目:基于深度学习的图像风格迁移 - [ 卷积神经网络 机器视觉 ]
    vue项目打包成dist文件夹之后后端请求报404错误解决方法
    stable diffusion模型评价框架
    用于实体和关系抽取的封装式悬空标记
    Python 机器学习 决策树 分类原理
  • 原文地址:https://blog.csdn.net/qq_34682765/article/details/133660447