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

  • 相关阅读:
    详解连接池参数设置(边调边看)
    电子章盖章软件怎么找?从需求、功能、成本、口碑四方面入手
    【炫丽】从0开始做一个WPF+Blazor对话小程序
    Blender 学习笔记(二)之坐标
    举例说明用 easylanguage 语言,编写抄底公式
    SpringCloud中的Eureka的集群配置
    基于Boost库的在线搜索引擎
    Auto.js中的一般全局函数
    GD32F103 硬件 IIC
    vue模板语法上集
  • 原文地址:https://blog.csdn.net/qq_34682765/article/details/133660447