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 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错误用法一例

时间: 2024-08-27 23:48:44

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

STL中map与hash_map容器的选择

[转]STL中map与hash_map容器的选择 先看看alvin_lee 朋友做的解析,我觉得还是很正确的,从算法角度阐述了他们之间的问题! 实际上这个问题不光C++会遇到,其他所有语言的标准容器的实现及选择上都是要考虑的.做应用程序你可能觉得影响不大,但是写算法或者核心代码就要小心了.今天改进代码,顺便又来温习基础功课了. 还记得Herb Sutter那极有味道的<C++对话系列>么,在其中<产生真正的hash对象>这个故事里就讲了map的选择.顺便回顾一下,也讲一下我在实用中

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

原文  http://blog.csdn.net/iicy266/article/details/11906189 map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区分),我们用map来进行存储就是个不错的选择. 我们这样定义,map<string, int>,其中学生姓名用string类型,作为Key:该学生的成绩用int类型,作为value.这样一来,我们可以根据学

STL中map与hash_map的比较

1. map : C++的STL中map是使用树来做查找算法; 时间复杂度:O(log2N) 2. hash_map : 使用hash表来排列配对,hash表是使用关键字来计算表位置; 时间复杂度:O(1), 最坏的时间复杂度:O(n) 总体来说:hash_map 比 map 查找速度快,而且查找速度基本和数据量大小无关,属于常数级别,节省一定内存,如果没有必要排序的话,尽量使用 hash_map . 注:hash还有hash函数的耗时.当有100w条记录的时候,map也只需要20次的比较,20

POJ 2418 Hardwood Species(STL中map的应用)

题目地址:POJ 2418 通过这个题查了大量资料..知道了很多以前不知道的东西.... 在代码中注释说明吧. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue&

C++ STL中Map的按Key排序跟按Value排序(转)

C++ STL中Map的按Key排序和按Value排序 map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区 分),我们用map来进行存储就是个不错的选择. 我们这样定义,map<string, int>,其中学生姓名用string类型,作为Key:该学生的成绩用int类型,作为value.这样一来,我们可以根据学生姓名快速的查找到 他的成绩. 但是,我们除了希望能够查询

STL中map容器使用自定义key类型报错详解

引言 STL的map容器中,key的类型是不是随意的呢? 实践 编写测试代码 定义一个结构体来试试: struct a { char* pName; int m_a; }; map<a, int> mp; a a1; a1.m_a = 100; a1.pName = "a1"; a a2; a2.m_a = 200; a2.pName = "a2"; mp.insert(std::make_pair(a1, 1)); mp.insert(std::mak

STL中map的用法

map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道.这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处. 下面举例说明什么是一对一的数据映射.比如一个班级中,每个学生的学号跟他的姓名就存在着一一

STL中map的遍历

map作为STL中的映射容器非常好用,我们来说一下map的遍历. map.first为key值,map.second为value值,key不可修改,value可修改. 定义一个迭代指针iter,使其指向map,实现对map的遍历. 1 #include <iostream> 2 #include <map> 3 #include <string> 4 5 using namespace std; 6 7 int main() 8 { 9 map<string,in

stl中map自定义比较函数

在stl中自定义map比较函数有两种方式,方式一在类中重载<操作符,方式二仿比较函数 1.方式一:重载<操作符 #include "stdafx.h" #include <map> #include <string> #include <iostream> using namespace std; class Key { public: Key(string name, int age) { this->name=name; thi