使用std :: map的问题 [英] Problem using std::map

查看:99
本文介绍了使用std :: map的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用std :: map如下。


typedef struct _SeatRowCols

{

long nSeatRow;

unsigned char ucSeatLetter;

} SeatRowCols;


typedef struct _NetData

{

SeatRowCols tSeatRowCols;

unsigned char ucMacAddress [MAC_LENGTH];

unsigned char ucAdminStatus;

unsigned char ucLocalFault;


} NetData;


typedef std :: map< SeatRowCols,PedData> MAP_PED_DATA;

..........

.............


MAP_PED_DATA数据;

PedData pdata;

SeatRowCols sdata;


memset((void *)& sdata, 0,sizeof(SeatRowCols));

sdata.nSeatRow = 1;

sdata.ucSeatLetter =''A'';

memset( (void *)& pdata,0,sizeof(PedData));

pdata.nPortClass = 1;

pdata.tSeatRowCols = sdata;

data [sdata] = pdata;


在编译此代码时发生以下错误。


编译...

DemoOfDatatStructures.cpp

E:\ testprjs \ testit \ DemoOfDatatStructures\DemoOfDat atStructures.cpp(79)

:警告C4786:

''std :: reverse_bidirectional_iterator< std :: _ Tree< _S eatRowCols,std :: pair< _SeatRowCols

const

,_PedData> ;,std: :map< _SeatRowCols,_PedData,std :: les s< _SeatRowCols> ;, std ::

allocator< _PedData> :: _ Kfn,std :: less< _SeatRowCols> ;,std :: allocator< _Pe dData> > :: iterator,std :: pair< _SeatRowCols const,_PedData> ;, std :: pair< _SeatRowCols const,_PedData> &,std :: pair< _SeatRowCols const,_PedData> *,int>'':标识符在调试信息中被截断为
''255''字符

E:\ testprjs \ testit \ DemoOfDatatStructures \ DemoOfDat atStructures.cpp (79)

:警告C4786:

''std :: reverse_bidirectional_iterator< std :: _ Tree< _S eatRowCols,std :: pair< _SeatRowCols

const

,_PedData> ;,std :: map< _SeatRowCols,_PedData,std :: les s< _SeatRowCols> ;, std ::

allocator< _PedData> :: _Kfn,std :: less< _SeatRowCols> ;,std :: allocator< _Pe dData> > :: const_iterator,std :: pair< _SeatRowCols const,_PedData> ;, std :: pair< _SeatRowCols const,_PedData> const&,std :: pair< _SeatRowCols const,_PedData> const *,int>'':标识符
在调试信息中被截断为255个字符

E:\ testprjs \ testit \DemoOfDatatStructures \DemoOfDat atStructures。 cpp(79)

:警告C4786:

''std :: pair< std :: _ Tree< _SeatRowCols,std :: pair< _Seat RowCols const

,_PedData> ;,std :: map< _SeatRowCols,_PedData,std :: les s< _SeatRowCols> ;,std :: allocator< _PedData> :: _ Kf
n,std :: less< _SeatRowCols> ;,std :: allocator< _PedData> :: iterator,std :: _ Tree< _SeatRowCols,std :: pair< _Sea tRowCols const,_PedData> ;, std :: map< _SeatRowCols,_PedData,std :: les s< _SeatRowCols> ;,std ::分配器< _PedData> > :: _ Kfn,std :: less< _SeatRowCols> ;, std :: alloc
ator< _PedData> > :: iterator>'':标识符被截断为''255''

调试信息中的字符

E:\ testprjs \ testit \ DemoOfDatatStructures \ DemoOfDat atStructures.cpp(79)

:警告C4786:

''std :: pair< std :: _ Tree< _SeatRowCols,std :: pair< _Seat RowCols const

,_PedData> ;,std :: map< _SeatRowCols,_PedData,std :: les s< _SeatRowCols> ;,std :: allocator< _PedData> :: _ Kf
n,std :: less< ; _SeatRowCols> ;,std :: allocator< _PedData> :: const_iterator,std :: _ Tree< _SeatRowCols,std :: pai r< _SeatRowCols const,_PedData> ;, std :: map< _SeatRowCols,_PedData,std :: les s< _SeatRowCols> ;,的std ::分配器< _PedData> > :: _ Kfn,std :: less< _SeatRowCols> ;,std:



:allocator< _PedData> > :: const_iterator>'':标识符被截断为

''255''字符在调试信息中

e:\program files \ microsoft visual studio \\ \\ vc98 \include \ functional(86):

错误C2784:''bool __cdecl std :: operator<(const class

std :: multimap< _K ,_Ty,_Pr,_A>&,const class std :: multimap< _K,_Ty,_Pr,_A>

&)'':无法推断''const
class std :: multimap< _K,_Ty,_Pr,_A> &''来自''const struct _SeatRowCols''

e:\program files \ microsoft visual

studio\vc98\include\ functional(86) :编译类模板时

成员函数''bool __thiscall std :: less< struct

_SeatRowCols> :: operator()(const struct _SeatRowCols&,const struct

_SeatRowCols&)const

''

e:\ program program \ microsros visual studio\vc98 \include \ functional (86):

错误C2784:''bool __cdecl std :: operator<(const class

std :: map< _K,_Ty,_Pr,_A>& ;,const class std :: map< _K,_Ty,_Pr,_A>&)'':

无法推断''const class std的模板参数

::地图< _K,_Ty,_Pr,_A> &''来自''const struct _SeatRowCols''

e:\program files \ microsoft visual

studio\vc98\include\ functional(86) :编译类模板时

成员函数''bool __thiscall std :: less< struct

_SeatRowCols> :: operator()(const struct _SeatRowCols&,const struct

_SeatRowCols&)const

''

e:\ program program \ microsros visual studio\vc98 \include \ functional (86):

错误C2784:''bool __cdecl std :: operator<(const class

std :: _ Tree< _K,_Ty,_Kfn,_Pr,_A> ;&,const class

std :: _ Tree< _K,_Ty,_Kfn,_Pr,_A>&)'':无法推断模板参数

for' 'c

onst class std :: _ Tree< _K,_Ty,_Kfn,_Pr,_A> &''来自''const struct

_SeatRowCols''

e:\program files \ microsoft visual

studio\vc98\\ \\include\functional(86):同时编译类模板

成员函数''bool __thiscall std :: less< struct

_SeatRowCols> :: operator()( const struct _SeatRowCols&,const struct

_SeatRowCols&)const

''

e:\ program Files \ microsoft visual studio \\ \\ vc98 \include \ functional(86):

错误C2784:''bool __cdecl std :: operator<(const class

std :: reverse_iterator< _RI ,_Ty,_Rt,_Pt,_D>&,const class

std :: reverse_iterator< _RI,_Ty,_Rt,_Pt,_D>&)'':无法推断te 'b $ b mplate''const class

std :: reverse_iterator< _RI,_Ty,_Rt,_Pt,_D> &''来自''const struct

_SeatRowCols''

e:\program files \ microsoft visual

studio\vc98\\ \\include\functional(86):同时编译类模板

成员函数''bool __thiscall std :: less< struct

_SeatRowCols> :: operator()( const struct _SeatRowCols&,const struct

_SeatRowCols&)const

''

e:\ program Files \ microsoft visual studio \\ \\ vc98 \include \ functional(86):

错误C2784:''bool __cdecl std :: operator<(const struct

std :: pair< _T1 ,_T2>&,const struct std :: pair< _T1,_T2>&)'':无法
推断''const struct std :: pair<
的模板参数
_T1,_T2> &''来自''const struct _SeatRowCols''

e:\program files \ microsoft visual

studio\vc98\include\ functional(86) :编译类模板时

成员函数''bool __thiscall std :: less< struct

_SeatRowCols> :: operator()(const struct _SeatRowCols&,const struct

_SeatRowCols&)const

''

e:\ program program \ microsros visual studio\vc98 \include \ functional (86):

错误C2784:''bool __cdecl运算符<(_接口*,类

_com_ptr_t< _InterfacePtr>&)'':无法推断出模板参数for

''*''来自''const struct _SeatRowCols''

e:\program files \ microsoft visual

studio\ vc98\include\functional(86):编译类模板时

成员函数''bool __thiscall std :: less< struct

_SeatRow Cols> :: operator()(const struct _SeatRowCols&,const struct

_SeatRowCols&)const

''

e:\程序files\microsoft visual studio \vc98 \include \ functional(86):

error C2784:''bool __cdecl operator<(int,class _com_ptr_t< _Interface>

&)'':无法推断''重载函数类型'的模板参数''

来自''重载函数类型''

e:\程序文件\ microsoft visual

studio \vc98 \include \ functional(86):同时编译类模板

成员函数''bool __thiscall std :: less< struct

_SeatRowCols> :: operator()(const struct _SeatRowCols&,const struct

_SeatRowCols&)const

''

e:\program files\microsoft visual studio \vc98 \include \ functional(86):

error C2676:binary''<'': const struct _SeatRowCols''没有定义

这个运算符或转换为预定义

运算符可接受的类型

e:\ program program \ microsoft visual

studio\vc98\include\functional(86):同时编译类模板

成员函数''bool __thiscall std :: less< struct

_SeatRowCols> :: operator()(const struct _SeatRowCols&,const struct

_SeatRowCols&)const

''

执行cl.exe时出错。

请给我一些建议。


问候

kamaraj.M

解决方案


ka *******@gmail.com 写道:

您好
我正在使用std :: map如下。

typedef struct _SeatRowCols
{
long nSeatRow;
unsigned char ucSeatLetter;
} SeatRowCols;

typedef st ruct _NetData
{
SeatRowCols tSeatRowCols;
unsigned char ucMacAddress [MAC_LENGTH];
unsigned char ucAdminStatus;
unsigned char ucLocalFault;

} NetData;


这是非常C的定义结构的方法。那不是说

这是错的,也许你是在写一个可以用于C和C ++的库。如果那是

不是这样的话那么也许你应该使用C ++类。

typedef std :: map< SeatRowCols,PedData> MAP_PED_DATA;


我不知道PedData是什么,但无论如何std :: map是一个有序的

容器它需要一个手段

用于排序。您有4种方法可以实现这一目标:


1.重载运算符<为您的struct SeatRowCols。

2.为SeatRowCols专门为std :: less。

3.编写自己的比较函数或函子并将其传递给

std :: map作为第三个参数。

如果有多种方法可以对

SeatRowCols进行排序,那么最后一种方法是首选。

4(不建议):为double()或long()

提供运算符重载,返回可排序的值。

来自其余代码看来你用C语写了



你使用std :: map。虽然

我同意你可能想要为库提供一个C接口,但我会建议你选择使用C ++实现
,你通过实现使用C ++

除非你达到一个性能危急的情况,其中分析显示C

的方法是更快(或者如果C方法

更清晰,在你的情况下它不是大部分时间)。

抱歉,一个小的修正,结构名称不是NetData,即

PedData


typedef struct _PedData

{

SeatRowCols tSeatRowCols;

unsigned char ucMacAddress [MAC_LENGTH];

unsigned char ucAdminStatus;

unsigned char ucLocalFault;

} PedData


ka ******* @ gmail.com 写道:

对不起,一个小小的修正,str ucture名称不是NetData,即PedData

typedef struct _PedData
{SeatRowCols tSeatRowCols;
unsigned char ucMacAddress [MAC_LENGTH];
unsigned char ucAdminStatus;
unsigned char ucLocalFault;

} PedData




不要在标识符前加下划线。通常

标识符将保留用于实现。只需坚持

下划线,虽然这通常是私人

成员变量的惯例。


例如,你甚至不需要识别结构,因为你是

键入它:


typedef struct {

SeatRowCols tSeatRowCols;

unsigned char ucMacAddress [MAC_LENGTH];

unsigned char ucAdminStatus;

unsigned char ucLocalFault;

} PedData;


如果这仅用于C ++代码(而不是C),您可以更喜欢以下内容:


struct PedData {

SeatRowCols tSeatRowCols;

unsigned char ucMacAddress [MAC_LENGTH];

unsigned char ucAdminStatus;

unsigned char ucLocalFault ;

};


Ben Pope

-

我不只是一个数。对很多人来说,我被称为字符串...


Hi
I am using the std:: map as following.

typedef struct _SeatRowCols
{
long nSeatRow;
unsigned char ucSeatLetter;
}SeatRowCols;

typedef struct _NetData
{
SeatRowCols tSeatRowCols;
unsigned char ucMacAddress[MAC_LENGTH];
unsigned char ucAdminStatus;
unsigned char ucLocalFault;

}NetData;

typedef std::map <SeatRowCols, PedData> MAP_PED_DATA;
..........
.............

MAP_PED_DATA data;
PedData pdata;
SeatRowCols sdata;

memset((void*)&sdata,0,sizeof(SeatRowCols));
sdata.nSeatRow =1;
sdata.ucSeatLetter =''A'';
memset((void*)&pdata,0,sizeof(PedData));
pdata.nPortClass = 1;
pdata.tSeatRowCols = sdata;
data[sdata] = pdata;

while compiling this code the following error are occurs.

Compiling...
DemoOfDatatStructures.cpp
E:\testprjs\testit\DemoOfDatatStructures\DemoOfDat atStructures.cpp(79)
: warning C4786:
''std::reverse_bidirectional_iterator<std::_Tree<_S eatRowCols,std::pair<_SeatRowCols
const
,_PedData>,std::map<_SeatRowCols,_PedData,std::les s<_SeatRowCols>,std::
allocator<_PedData>

::_Kfn,std::less<_SeatRowCols>,std::allocator<_Pe dData> >::iterator,std::pair<_SeatRowCols const ,_PedData>,std::pair<_SeatRowCols const ,_PedData> &,std::pair<_SeatRowCols const ,_PedData> *,int>'' : identifier was truncated to ''255'' characters in the debug information
E:\testprjs\testit\DemoOfDatatStructures\DemoOfDat atStructures.cpp(79)
: warning C4786:
''std::reverse_bidirectional_iterator<std::_Tree<_S eatRowCols,std::pair<_SeatRowCols
const
,_PedData>,std::map<_SeatRowCols,_PedData,std::les s<_SeatRowCols>,std::
allocator<_PedData>::_Kfn,std::less<_SeatRowCols>,std::allocator<_Pe dData> >::const_iterator,std::pair<_SeatRowCols const ,_PedData>,std::pair<_SeatRowCols const ,_PedData> const &,std::pair<_SeatRowCols const ,_PedData> const *,int>'' : identifier was truncated to ''255'' characters in the debug information
E:\testprjs\testit\DemoOfDatatStructures\DemoOfDat atStructures.cpp(79)
: warning C4786:
''std::pair<std::_Tree<_SeatRowCols,std::pair<_Seat RowCols const
,_PedData>,std::map<_SeatRowCols,_PedData,std::les s<_SeatRowCols>,std::allocator<_PedData>::_Kf n,std::less<_SeatRowCols>,std::allocator<_PedData>::iterator,std::_Tree<_SeatRowCols,std::pair<_Sea tRowCols const ,_PedData>,std::map<_SeatRowCols,_PedData,std::les s<_SeatRowCols>,std::allocator<_PedData> >::_Kfn,std::less<_SeatRowCols>,std::alloc ator<_PedData> >::iterator>'' : identifier was truncated to ''255''
characters in the debug information
E:\testprjs\testit\DemoOfDatatStructures\DemoOfDat atStructures.cpp(79)
: warning C4786:
''std::pair<std::_Tree<_SeatRowCols,std::pair<_Seat RowCols const
,_PedData>,std::map<_SeatRowCols,_PedData,std::les s<_SeatRowCols>,std::allocator<_PedData>::_Kf n,std::less<_SeatRowCols>,std::allocator<_PedData>::const_iterator,std::_Tree<_SeatRowCols,std::pai r<_SeatRowCols const ,_PedData>,std::map<_SeatRowCols,_PedData,std::les s<_SeatRowCols>,std::allocator<_PedData> >::_Kfn,std::less<_SeatRowCols>,std:


:allocator<_PedData> >::const_iterator>'' : identifier was truncated to
''255'' characters in the debug information
e:\program files\microsoft visual studio\vc98\include\functional(86) :
error C2784: ''bool __cdecl std::operator <(const class
std::multimap<_K,_Ty,_Pr,_A> &,const class std::multimap<_K,_Ty,_Pr,_A>
&)'' : could not deduce template argument for ''const
class std::multimap<_K,_Ty,_Pr,_A> &'' from ''const struct _SeatRowCols''
e:\program files\microsoft visual
studio\vc98\include\functional(86) : while compiling class-template
member function ''bool __thiscall std::less<struct
_SeatRowCols>::operator ()(const struct _SeatRowCols &,const struct
_SeatRowCols &) const
''
e:\program files\microsoft visual studio\vc98\include\functional(86) :
error C2784: ''bool __cdecl std::operator <(const class
std::map<_K,_Ty,_Pr,_A> &,const class std::map<_K,_Ty,_Pr,_A> &)'' :
could not deduce template argument for ''const class std
::map<_K,_Ty,_Pr,_A> &'' from ''const struct _SeatRowCols''
e:\program files\microsoft visual
studio\vc98\include\functional(86) : while compiling class-template
member function ''bool __thiscall std::less<struct
_SeatRowCols>::operator ()(const struct _SeatRowCols &,const struct
_SeatRowCols &) const
''
e:\program files\microsoft visual studio\vc98\include\functional(86) :
error C2784: ''bool __cdecl std::operator <(const class
std::_Tree<_K,_Ty,_Kfn,_Pr,_A> &,const class
std::_Tree<_K,_Ty,_Kfn,_Pr,_A> &)'' : could not deduce template argument
for ''c
onst class std::_Tree<_K,_Ty,_Kfn,_Pr,_A> &'' from ''const struct
_SeatRowCols''
e:\program files\microsoft visual
studio\vc98\include\functional(86) : while compiling class-template
member function ''bool __thiscall std::less<struct
_SeatRowCols>::operator ()(const struct _SeatRowCols &,const struct
_SeatRowCols &) const
''
e:\program files\microsoft visual studio\vc98\include\functional(86) :
error C2784: ''bool __cdecl std::operator <(const class
std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &,const class
std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)'' : could not deduce te
mplate argument for ''const class
std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &'' from ''const struct
_SeatRowCols''
e:\program files\microsoft visual
studio\vc98\include\functional(86) : while compiling class-template
member function ''bool __thiscall std::less<struct
_SeatRowCols>::operator ()(const struct _SeatRowCols &,const struct
_SeatRowCols &) const
''
e:\program files\microsoft visual studio\vc98\include\functional(86) :
error C2784: ''bool __cdecl std::operator <(const struct
std::pair<_T1,_T2> &,const struct std::pair<_T1,_T2> &)'' : could not
deduce template argument for ''const struct std::pair<
_T1,_T2> &'' from ''const struct _SeatRowCols''
e:\program files\microsoft visual
studio\vc98\include\functional(86) : while compiling class-template
member function ''bool __thiscall std::less<struct
_SeatRowCols>::operator ()(const struct _SeatRowCols &,const struct
_SeatRowCols &) const
''
e:\program files\microsoft visual studio\vc98\include\functional(86) :
error C2784: ''bool __cdecl operator <(_Interface *,class
_com_ptr_t<_InterfacePtr> &)'' : could not deduce template argument for
'' *'' from ''const struct _SeatRowCols''
e:\program files\microsoft visual
studio\vc98\include\functional(86) : while compiling class-template
member function ''bool __thiscall std::less<struct
_SeatRowCols>::operator ()(const struct _SeatRowCols &,const struct
_SeatRowCols &) const
''
e:\program files\microsoft visual studio\vc98\include\functional(86) :
error C2784: ''bool __cdecl operator <(int,class _com_ptr_t<_Interface>
&)'' : could not deduce template argument for ''overloaded function type''
from ''overloaded function type''
e:\program files\microsoft visual
studio\vc98\include\functional(86) : while compiling class-template
member function ''bool __thiscall std::less<struct
_SeatRowCols>::operator ()(const struct _SeatRowCols &,const struct
_SeatRowCols &) const
''
e:\program files\microsoft visual studio\vc98\include\functional(86) :
error C2676: binary ''<'' : ''const struct _SeatRowCols'' does not define
this operator or a conversion to a type acceptable to the predefined
operator
e:\program files\microsoft visual
studio\vc98\include\functional(86) : while compiling class-template
member function ''bool __thiscall std::less<struct
_SeatRowCols>::operator ()(const struct _SeatRowCols &,const struct
_SeatRowCols &) const
''
Error executing cl.exe.
Please gimme some suggestions.

Regards
kamaraj.M

解决方案


ka*******@gmail.com wrote:

Hi
I am using the std:: map as following.

typedef struct _SeatRowCols
{
long nSeatRow;
unsigned char ucSeatLetter;
}SeatRowCols;

typedef struct _NetData
{
SeatRowCols tSeatRowCols;
unsigned char ucMacAddress[MAC_LENGTH];
unsigned char ucAdminStatus;
unsigned char ucLocalFault;

}NetData;

That is very much a "C" way of defining a struct. That''s not to say
it''s wrong, perhaps you
are writing a library that can be used for C as well as C++. If that is
not the case then perhaps
you should use C++ classes.
typedef std::map <SeatRowCols, PedData> MAP_PED_DATA;
I don''t know what PedData is but in any case as std::map is a sorted
container it needs a means
by which to sort. You have 4 ways to achieve this:

1. Overload operator < for your struct SeatRowCols.
2. Specialise std::less for SeatRowCols.
3. Write your own comparison function or functor and pass it to
std::map as the 3rd parameter.
This last method is preferred if there''s more than one way to sort a
SeatRowCols.
4 (not advised): Provide an operator overload for double() or long()
that returns a sortable value.
From the rest of the code it appears you are writing in C apart from


your use of std::map. Although
I agree you might want to provide a C interface to a library, I would
suggest that once you choose to
implement in C++, you use C++ all the way through the implementation
unless you reach a
performance-critical situation where profiling shows that the C
approach is faster (or if the C approach
is a lot clearer, which in your case it isn''t most of the time).


Sorry, a small correction, the structure name is not NetData, that is
PedData

typedef struct _PedData
{
SeatRowCols tSeatRowCols;
unsigned char ucMacAddress[MAC_LENGTH];
unsigned char ucAdminStatus;
unsigned char ucLocalFault;
}PedData


ka*******@gmail.com wrote:

Sorry, a small correction, the structure name is not NetData, that is
PedData

typedef struct _PedData
{
SeatRowCols tSeatRowCols;
unsigned char ucMacAddress[MAC_LENGTH];
unsigned char ucAdminStatus;
unsigned char ucLocalFault;
}PedData



Prefer not to prefix identifiers with an underscore. Often the
identifier will be reserved for the implementation. Just stick the
underscore on the end, although that''s often convention for private
member variables.

In this case, you don''t even need to identify the struct, since you are
typedefing it:

typedef struct {
SeatRowCols tSeatRowCols;
unsigned char ucMacAddress[MAC_LENGTH];
unsigned char ucAdminStatus;
unsigned char ucLocalFault;
} PedData;

If this is only used in C++ code (and not C), you can prefer the following:

struct PedData {
SeatRowCols tSeatRowCols;
unsigned char ucMacAddress[MAC_LENGTH];
unsigned char ucAdminStatus;
unsigned char ucLocalFault;
};

Ben Pope
--
I''m not just a number. To many, I''m known as a string...


这篇关于使用std :: map的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆