【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 files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: “bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)”: 未能从“const TX_DATA”为“const std::_Tree<_Traits> &”推导 模板 参数
1> c:\program files\microsoft visual studio 10.0\vc\include\xtree(1885) : 参见“std::operator <”的声明
1> c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(124): 编译类 模板 成员函数“bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const”时
1> with
1> [
1> _Ty=TX_DATA
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\map(71): 参见对正在编译的类 模板 实例化“std::less<_Ty>”的引用
1> with
1> [
1> _Ty=TX_DATA
1> ]
1> c:\program files\microsoft visual studio 10.0\vc\include\xtree(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 files\microsoft visual studio 10.0\vc\include\xtree(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 files\microsoft visual studio 10.0\vc\include\xtree(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 files\microsoft visual studio 10.0\vc\include\map(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:\reverse\阿福迪脱机\simulator\dll\globalvars.h(6): 参见对正在编译的类 模板 实例化“std::map<_Kty,_Ty>”的引用
1> with
1> [
1> _Kty=TX_DATA,
1> _Ty=RX_DATA
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(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 files\microsoft visual studio 10.0\vc\include\memory(2582) : 参见“std::operator <”的声明
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(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 files\microsoft visual studio 10.0\vc\include\xutility(1356) : 参见“std::operator <”的声明
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(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 files\microsoft visual studio 10.0\vc\include\xutility(1179) : 参见“std::operator <”的声明
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(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 files\microsoft visual studio 10.0\vc\include\utility(318) : 参见“std::operator <”的声明
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(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碰到'\0'不会截断的,是吧
【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
不会根据 \0 截断
【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
遇上问题再说 :)

STL中map错误用法一例的更多相关文章

  1. stl中map的四种插入方法总结

    stl中map的四种插入方法总结方法一:pair例:map<int, string> mp;mp.insert(pair<int,string>(1,"aaaaa&q ...

  2. STL中map与hash_map容器的选择收藏

    这篇文章来自我今天碰到的一个问题,一个朋友问我使用map和hash_map的效率问题,虽然我也了解一些,但是我不敢直接告诉朋友,因为我怕我说错了,通过我查询一些帖子,我这里做一个总结!内容分别来自al ...

  3. C++ STL 中 map 容器

    C++ STL 中 map 容器 Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力,由于这个特性,它 ...

  4. C++ STL中Map的按Key排序和按Value排序

    map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区 分),我们用map来进 ...

  5. STL中map与hash_map的比较

    1. map : C++的STL中map是使用树来做查找算法; 时间复杂度:O(log2N) 2. hash_map : 使用hash表来排列配对,hash表是使用关键字来计算表位置; 时间复杂度:O ...

  6. C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET

    C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET C++ STL中Map的相关排序操作:按Key排序和按Value排序 分类: C ...

  7. C++中的STL中map用法详解(转)

    原文地址: https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html C++中的STL中map用法详解   Map是STL的一个关联容器,它提供 ...

  8. C++ STL中Map的按Key排序跟按Value排序

    C++ STL中Map的按Key排序和按Value排序 map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定 ...

  9. STL中map的使用

    知识点 C++中map提供的是一种键值对容器,里面的数据都是成对出现的.map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的. ...

随机推荐

  1. Can a windows dll retrieve its own filename?

    http://stackoverflow.com/questions/2043/can-a-windows-dll-retrieve-its-own-filename A windows exe fi ...

  2. stdafx.h是什么用处, stdafx.h、stdafx.cpp的作用

    http://blog.csdn.net/songkexin/article/details/1750396 stdafx.h头文件的作用 Standard Application Fram Exte ...

  3. spring boot配置springMVC拦截器

    spring boot通过配置springMVC拦截器 配置拦截器比较简单, spring boot配置拦截器, 重写preHandle方法. 1.配置拦截器: 2重写方法 这样就实现了拦截器. 其中 ...

  4. 判断openfire用户的状态

    /** * 判断openfire用户的状态 * 说明 :必须要 openfire加载 presence 插件,同时设置任何人都可以访问 * /status?jid=user1@my.openfire. ...

  5. 解析天气预报JSON数据

    解析天气预报JSON数据 JSON字符串 constjson2 = '{' + #13#10 +'"error":0,' + #13#10 +'"status" ...

  6. pytest文档9-参数化parametrize

    前言 pytest.mark.parametrize装饰器可以实现测试用例参数化. parametrizing 1.这里是一个实现检查一定的输入和期望输出测试功能的典型例子 # content of ...

  7. UIBezierPath的使用(持续更新)

    UIBezierPath的使用 1. 使用UIBezierPath绘制多边形 // 获取path UIBezierPath *aPath = [UIBezierPath bezierPath]; // ...

  8. Computer Vision Tutorials from Conferences (3) -- CVPR

    CVPR 2013 (http://www.pamitc.org/cvpr13/tutorials.php) Foundations of Spatial SpectroscopyJames Cogg ...

  9. cocos2d-x v3.0新特性及使用

    八月份cocos2d-x官网发布了v3.0版本,这次更新的内容特别多,包括2dx的架构以及使用 总得来说,给开发者带来了很大的便利: 运行环境需求: Android 2.3 or newer iOS ...

  10. 使用Vue.js制作仿Metronic高级表格(一)静态设计

    Metronic高级表格是Metonic框架中自行实现的表格,其底层是Datatables.本教程将主要使用Vue实现交互部分,使用Bootstrap做样式库.jQuery做部分用户交互(弹窗). 使 ...