STL中map错误用法一例

【GBK】0G's fans( me ) 13:34:26
typedef struct _TX_DATA
{
int len;
unsigned char buff[0x100];

} TX_DATA, *PTX_DATA;


typedef struct _RX_DATA
{
int len;
unsigned char buff[0x100];

} RX_DATA, *PRX_DATA;

 

 

TX_DATA tx;
RX_DATA rx;
map<TX_DATA, RX_DATA> aMap;


aMap[tx] = rx;

 

这个代码错在哪里了呢?
【UTF-8】NewbieCoder's fans3< 夜枫 > 13:44:05
没初始化变量?
【GBK】0G's fans( me ) 13:44:35
tx,rx已经初始化了
【UTF-8】NewbieCoder's fans3< 夜枫 > 13:44:40
结构体能做KEY么
【UTF-8】NewbieCoder's fans3< 夜枫 > 13:44:46
要重载比较操作符吧
【GBK】0G's fans( me ) 13:45:51
结构体不能当做Key来用吗
【UTF-8】NewbieCoder's fans3< 夜枫 > 13:46:07
你是什么编译错误啊
【GBK】0G's fans( me ) 13:47:01
1>------ 已启动生成: 项目: DLL, 配置: Debug Win32 ------
1> Config.cpp
1>c:program filesmicrosoft visual studio 10.0vcincludexfunctional(125): error C2784: “bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)”: 未能从“const TX_DATA”为“const std::_Tree<_Traits> &”推导 模板 参数
1> c:program filesmicrosoft visual studio 10.0vcincludextree(1885) : 参见“std::operator <”的声明
1> c:program filesmicrosoft visual studio 10.0vcincludexfunctional(124): 编译类 模板 成员函数“bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const”时
1> with
1> [
1> _Ty=TX_DATA
1> ]
1> c:program filesmicrosoft visual studio 10.0vcincludemap(71): 参见对正在编译的类 模板 实例化“std::less<_Ty>”的引用
1> with
1> [
1> _Ty=TX_DATA
1> ]
1> c:program filesmicrosoft visual studio 10.0vcincludextree(451): 参见对正在编译的类 模板 实例化“std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,_Mfl>”的引用
1> with
1> [
1> _Kty=TX_DATA,
1> _Ty=RX_DATA,
1> _Pr=std::less<TX_DATA>,
1> _Alloc=std::allocator<std::pair<const TX_DATA,RX_DATA>>,
1> _Mfl=false
1> ]
1> c:program filesmicrosoft visual studio 10.0vcincludextree(520): 参见对正在编译的类 模板 实例化“std::_Tree_nod<_Traits>”的引用
1> with
1> [
1> _Traits=std::_Tmap_traits<TX_DATA,RX_DATA,std::less<TX_DATA>,std::allocator<std::pair<const TX_DATA,RX_DATA>>,false>
1> ]
1> c:program filesmicrosoft visual studio 10.0vcincludextree(659): 参见对正在编译的类 模板 实例化“std::_Tree_val<_Traits>”的引用
1> with
1> [
1> _Traits=std::_Tmap_traits<TX_DATA,RX_DATA,std::less<TX_DATA>,std::allocator<std::pair<const TX_DATA,RX_DATA>>,false>
1> ]
1> c:program filesmicrosoft visual studio 10.0vcincludemap(81): 参见对正在编译的类 模板 实例化“std::_Tree<_Traits>”的引用
1> with
1> [
1> _Traits=std::_Tmap_traits<TX_DATA,RX_DATA,std::less<TX_DATA>,std::allocator<std::pair<const TX_DATA,RX_DATA>>,false>
1> ]
1> f: everse阿福迪脱机simulatordllglobalvars.h(6): 参见对正在编译的类 模板 实例化“std::map<_Kty,_Ty>”的引用
1> with
1> [
1> _Kty=TX_DATA,
1> _Ty=RX_DATA
1> ]
1>c:program filesmicrosoft visual studio 10.0vcincludexfunctional(125): error C2784: “bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)”: 未能从“const TX_DATA”为“const std::unique_ptr<_Ty,_Dx> &”推导 模板 参数
1> c:program filesmicrosoft visual studio 10.0vcincludememory(2582) : 参见“std::operator <”的声明
1>c:program filesmicrosoft visual studio 10.0vcincludexfunctional(125): error C2784: “bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)”: 未能从“const TX_DATA”为“const std::reverse_iterator<_RanIt> &”推导 模板 参数
1> c:program filesmicrosoft visual studio 10.0vcincludexutility(1356) : 参见“std::operator <”的声明
1>c:program filesmicrosoft visual studio 10.0vcincludexfunctional(125): error C2784: “bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)”: 未能从“const TX_DATA”为“const std::_Revranit<_RanIt,_Base> &”推导 模板 参数
1> c:program filesmicrosoft visual studio 10.0vcincludexutility(1179) : 参见“std::operator <”的声明
1>c:program filesmicrosoft visual studio 10.0vcincludexfunctional(125): error C2784: “bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)”: 未能从“const TX_DATA”为“const std::pair<_Ty1,_Ty2> &”推导 模板 参数
1> c:program filesmicrosoft visual studio 10.0vcincludeutility(318) : 参见“std::operator <”的声明
1>c:program filesmicrosoft visual studio 10.0vcincludexfunctional(125): error C2676: 二进制“<”:“const TX_DATA”不定义该运算符或到预定义运算符可接收的类型的转换
【UTF-8】NewbieCoder's fans3< 夜枫 > 13:48:13
目测就是没有比较操作符
【GBK】0G's fans( me ) 13:48:30

【GBK】0G's fans( me ) 13:48:34
那我该怎么办呢
【UTF-8】NewbieCoder's fans3< 夜枫 > 13:48:55
std::string 不行么
【UTF-8】NewbieCoder's fans3< 夜枫 > 13:49:04
偏要写成一个len+buff的形式
【GBK】0G's fans( me ) 13:50:45
那就这样定义map了?

map<string, string> aMap;
【GBK】0G's fans( me ) 13:50:54
可是感觉这样后期使用不方便
【UTF-8】NewbieCoder's fans3< 夜枫 > 13:51:15
这个看你业务需要
【UTF-8】NewbieCoder's fans3< 夜枫 > 13:51:37
我是觉得 len+buffer这个形式一般没啥意义,除非做传输或者写文件
【GBK】0G's fans( me ) 13:52:43
业务就是,

给出一个len+buffer,找到对应的len+buffer
【UTF-8】NewbieCoder's fans3< 夜枫 > 13:53:12
我是说你内存里这样存数据没啥意义,二进制数据或者字符串的话完全可以用 std::string 存
【UTF-8】NewbieCoder's fans3< 夜枫 > 13:54:04
结构体做KEY的话要重载 operator < 和 == 吧应该
【UTF-8】NewbieCoder's fans3< 夜枫 > 13:54:07
我也没试过
【GBK】0G's fans( me ) 13:55:52
我知道错了,我还是用string的好


【UTF-8】NewbieCoder's fans3< 夜枫 > 13:56:19
知错能改,孺子可教也
【GBK】0G's fans( me ) 13:56:50
不过
string和unsigned char[]

二者怎么转换呢
【UTF-8】NewbieCoder's fans3< 夜枫 > 13:56:59
assign 也可以
【UTF-8】NewbieCoder's fans3< 夜枫 > 13:57:10
string str(buff, buff+len) 这样也可以
【GBK】0G's fans( me ) 13:58:00
string转unsigned char[] 呢
【UTF-8】NewbieCoder's fans3< 夜枫 > 13:58:13
str.data(), str.size()
【GBK】0G's fans( me ) 13:58:37
谢谢夜枫
【GBK】0G's fans( me ) 13:58:40
我去试试
【UTF-8】NewbieCoder's fans3< 夜枫 > 13:59:00

【GBK】0G's fans( me ) 13:59:52
string碰到''不会截断的,是吧
【GBK】0G's fans( me ) 14:00:06
所以我可以放心存二进制数据?
【UTF-8】NewbieCoder's fans3< 夜枫 > 14:00:13
.size() 才是他的长度
【UTF-8】NewbieCoder's fans3< 夜枫 > 14:00:19
不会根据 截断
【UTF-8】NewbieCoder's fans3< 夜枫 > 14:00:24
可以存二进制的
【GBK】0G's fans( me ) 14:00:32
真是太好了
【GBK】0G's fans( me ) 14:00:54

【UTF-8】NewbieCoder's fans3< 夜枫 > 14:01:33
不过存二进制有些地方稍微要注意下,具体啥地方我不记得了
【GBK】0G's fans( me ) 14:02:17
遇上问题再说 :)

原文地址:https://www.cnblogs.com/huhu0013/p/3919620.html