STL问题 [英] Problem with STL
问题描述
我正在为我的应用程序中的某些函数指针尝试一个双重间接查找表(是的,我有充分的理由)。
我正在尝试使用std :: map对于表但(因为双间接)我需要使用std :: map *
其中我不知道如何映射[index](用指针) )
现在也许我在滥用它,我不确定(我对STL一无所知,我对它的标题有一些了解)
>
无论如何这里是我的代码(问题用红色解释),如果你能给我一些提示.....
============= =
static std :: map< const char *,SELmap_sels;
static std :: map< OClass,std :: map< SEL,IMP> * map_imps;
void id :: GetImpAlways(const char * name,SEL& sel,IMP& imp)
{
if(! _handle)
抛出gcnew ObjectDisposedException(Class() - > Name);
if(map_sels.find(name)== map_sels.end() )
{
sel = sel_register_name(name);
map_sels [name] = sel;
}
else
{
sel = map_sels [name];
}
std :: map< SEL,IMP * class_imps; //这里我得到有问题的地图*
if(map_imps.find(_handle-> class_pointer)== map_imps.end())
{
class_imps = new std :: map< SEL,IMP>();
map_imps [_handle-> class_pointer] = class_imps;
}
其他
{
class_imps = map_imps [_handle-> class_pointer];
}
if(class_imps-> find(sel)== class_imps-> end())
{
imp = get_imp( _handle-> class_pointer,sel);
if(!imp)
throw gcnew ObjectiveCException(" No such method");
class_imps [sel] = imp; //不能用''map *''来做,我应该怎么做?
}
其他
{
imp = class_imps [sel]; //不能用''map *''来做,我该怎么办?
}
}
=== ===========
I''m trying a double indirect look-up table for some function pointer in my app (yep, I have good reason).
I''m trying to use std::map for the table but (because of the double indirection) I need to use a "std::map*"
Which I don''t know how to map[index] (with a pointer)
Now perhaps I''m misusing it, I''m not sure (I know nothing of STL and I have some problem understanding its header)
Anyway here is my code (problem explained in red), if you could give me some tips.....
==============
static std::map<const char*, SELmap_sels;
static std::map<OClass, std::map<SEL, IMP>*map_imps;
void id::GetImpAlways(const char* name, SEL& sel, IMP& imp)
{
if( !_handle )
throw gcnew ObjectDisposedException(Class()->Name);
if(map_sels.find(name) == map_sels.end())
{
sel = sel_register_name(name);
map_sels[name] = sel;
}
else
{
sel = map_sels[name];
}
std::map<SEL, IMP* class_imps; // here I get the problematic "map *"
if(map_imps.find(_handle->class_pointer) == map_imps.end())
{
class_imps = new std::map<SEL, IMP>();
map_imps[_handle->class_pointer] = class_imps;
}
else
{
class_imps = map_imps[_handle->class_pointer];
}
if(class_imps->find(sel) == class_imps->end())
{
imp = get_imp(_handle->class_pointer, sel);
if( !imp )
throw gcnew ObjectiveCException("No such method");
class_imps[sel] = imp; // can''t do that with ''map*'', what should I so?
}
else
{
imp = class_imps[sel]; // can''t do that with ''map*'', what should I so?
}
}
==============
推荐答案
BTW,别介意我的问题。
程序崩溃在第一行:if(map_sels.find(name)== map_sels.end())
我该怎么办?
我有一个自制的C哈希表我应该用它代替: - /
" Lloyd Dupont" < net.galador@ldwrote留言新闻:%2 **************** @ TK2MSFTNGP05.phx.gbl ...
我是在我的应用程序中为某些函数指针尝试一个双重间接查找表(是的,我有充分的理由)。
我正在尝试使用std :: map作为表但是(因为双重间接)我需要使用std :: map *
其中我不知道如何映射[index](用指针)
现在也许我在滥用它,我不确定(我对STL一无所知,我对它的标题有一些了解)
无论如何这里是我的代码(问题用红色解释),如果你能给我一些提示.....
==============
static std :: map< const char *,SELmap_sels;
static std :: map< OClass,std :: map< SEL,IMP> * map_imps;
void id :: GetImpAlways(const char * name,SEL& sel,IMP& imp)
{
if(!_handle)
throw gcnew ObjectDisposedException(Class() - > Nam e);
if(map_sels.find(name)== map_sels.end())
{
sel = sel_register_name(name);
map_sels [name] = sel;
}
else
{
sel = map_sels [name];
}
std :: map< SEL,IMP * class_imps; //这里我得到有问题的地图*
if(map_imps.find(_handle-> class_pointer)== map_imps.end())
{
class_imps = new std :: map< SEL,IMP>();
map_imps [_handle-> class_pointer] = class_imps;
}
其他
{
class_imps = map_imps [_handle-> class_pointer];
}
if(class_imps-> find(sel)== class_imps-> end())
{
imp = get_imp( _handle-> class_pointer,sel);
if(!imp)
throw gcnew ObjectiveCException(" No such method");
class_imps [sel] = imp; //不能用''map *''来做,我应该怎么做?
}
其他
{
imp = class_imps [sel]; //不能用''map *''来做,我该怎么办?
}
}
=== ===========
BTW, never mind my problem.
The program crash at the first line: if(map_sels.find(name) == map_sels.end())
How do I do?
I have a home made C hashtable possibly I should use it instead :-/
"Lloyd Dupont" <net.galador@ldwrote in message news:%2****************@TK2MSFTNGP05.phx.gbl...
I''m trying a double indirect look-up table for some function pointer in my app (yep, I have good reason).
I''m trying to use std::map for the table but (because of the double indirection) I need to use a "std::map*"
Which I don''t know how to map[index] (with a pointer)
Now perhaps I''m misusing it, I''m not sure (I know nothing of STL and I have some problem understanding its header)
Anyway here is my code (problem explained in red), if you could give me some tips.....
==============
static std::map<const char*, SELmap_sels;
static std::map<OClass, std::map<SEL, IMP>*map_imps;
void id::GetImpAlways(const char* name, SEL& sel, IMP& imp)
{
if( !_handle )
throw gcnew ObjectDisposedException(Class()->Name);
if(map_sels.find(name) == map_sels.end())
{
sel = sel_register_name(name);
map_sels[name] = sel;
}
else
{
sel = map_sels[name];
}
std::map<SEL, IMP* class_imps; // here I get the problematic "map *"
if(map_imps.find(_handle->class_pointer) == map_imps.end())
{
class_imps = new std::map<SEL, IMP>();
map_imps[_handle->class_pointer] = class_imps;
}
else
{
class_imps = map_imps[_handle->class_pointer];
}
if(class_imps->find(sel) == class_imps->end())
{
imp = get_imp(_handle->class_pointer, sel);
if( !imp )
throw gcnew ObjectiveCException("No such method");
class_imps[sel] = imp; // can''t do that with ''map*'', what should I so?
}
else
{
imp = class_imps[sel]; // can''t do that with ''map*'', what should I so?
}
}
==============
没关系,我会使用我自己的C Hashtable ......
" Lloyd杜邦" < net.galador@ldwrote留言新闻:%2 **************** @ TK2MSFTNGP05.phx.gbl ...
我是在我的应用程序中为某些函数指针尝试一个双重间接查找表(是的,我有充分的理由)。
我正在尝试使用std :: map作为表但是(因为双重间接)我需要使用std :: map *
其中我不知道如何映射[index](用指针)
现在也许我在滥用它,我不确定(我对STL一无所知,我对它的标题有一些了解)
无论如何这里是我的代码(问题用红色解释),如果你能给我一些提示.....
==============
static std :: map< const char *,SELmap_sels;
static std :: map< OClass,std :: map< SEL,IMP> * map_imps;
void id :: GetImpAlways(const char * name,SEL& sel,IMP& imp)
{
if(!_handle)
throw gcnew ObjectDisposedException(Class() - > Nam e);
if(map_sels.find(name)== map_sels.end())
{
sel = sel_register_name(name);
map_sels [name] = sel;
}
else
{
sel = map_sels [name];
}
std :: map< SEL,IMP * class_imps; //这里我得到有问题的地图*
if(map_imps.find(_handle-> class_pointer)== map_imps.end())
{
class_imps = new std :: map< SEL,IMP>();
map_imps [_handle-> class_pointer] = class_imps;
}
其他
{
class_imps = map_imps [_handle-> class_pointer];
}
if(class_imps-> find(sel)== class_imps-> end())
{
imp = get_imp( _handle-> class_pointer,sel);
if(!imp)
throw gcnew ObjectiveCException(" No such method");
class_imps [sel] = imp; //不能用''map *''来做,我应该怎么做?
}
其他
{
imp = class_imps [sel]; //不能用''map *''来做,我该怎么办?
}
}
=== ===========
never mind, I wil use my own C Hashtable...
"Lloyd Dupont" <net.galador@ldwrote in message news:%2****************@TK2MSFTNGP05.phx.gbl...
I''m trying a double indirect look-up table for some function pointer in my app (yep, I have good reason).
I''m trying to use std::map for the table but (because of the double indirection) I need to use a "std::map*"
Which I don''t know how to map[index] (with a pointer)
Now perhaps I''m misusing it, I''m not sure (I know nothing of STL and I have some problem understanding its header)
Anyway here is my code (problem explained in red), if you could give me some tips.....
==============
static std::map<const char*, SELmap_sels;
static std::map<OClass, std::map<SEL, IMP>*map_imps;
void id::GetImpAlways(const char* name, SEL& sel, IMP& imp)
{
if( !_handle )
throw gcnew ObjectDisposedException(Class()->Name);
if(map_sels.find(name) == map_sels.end())
{
sel = sel_register_name(name);
map_sels[name] = sel;
}
else
{
sel = map_sels[name];
}
std::map<SEL, IMP* class_imps; // here I get the problematic "map *"
if(map_imps.find(_handle->class_pointer) == map_imps.end())
{
class_imps = new std::map<SEL, IMP>();
map_imps[_handle->class_pointer] = class_imps;
}
else
{
class_imps = map_imps[_handle->class_pointer];
}
if(class_imps->find(sel) == class_imps->end())
{
imp = get_imp(_handle->class_pointer, sel);
if( !imp )
throw gcnew ObjectiveCException("No such method");
class_imps[sel] = imp; // can''t do that with ''map*'', what should I so?
}
else
{
imp = class_imps[sel]; // can''t do that with ''map*'', what should I so?
}
}
==============
Lloyd Dupont写道:
Lloyd Dupont wrote:
我是尝试使用双重间接查找表来获取某些函数指针
我的应用程序(是的,我有充分的理由)。
我正在尝试使用std ::该表的地图但是(由于双重
间接)我需要使用std :: map *
其中我不知道如何地图[索引](带指针)
现在也许我在滥用它,我不确定(我对STL和我一无所知
了解其标题有一些问题)
无论如何这里是我的代码(问题用红色解释),如果你能给我
一些提示。 ....
==============
static std :: map< const char *,SELmap_sels;
I''m trying a double indirect look-up table for some function pointer in
my app (yep, I have good reason).
I''m trying to use std::map for the table but (because of the double
indirection) I need to use a "std::map*"
Which I don''t know how to map[index] (with a pointer)
Now perhaps I''m misusing it, I''m not sure (I know nothing of STL and I
have some problem understanding its header)
Anyway here is my code (problem explained in red), if you could give me
some tips.....
==============
static std::map<const char*, SELmap_sels;
这是你的第一个问题。这将基于指针值查找,而不是
字符串值。相反,你想要:
#include< cstring>
struct CharStringLess
{
bool operator()(char const * lhs,char const * rhs)const
{
return std :: strcmp(lhs,rhs)< 0;
}
};
static std :: map< const char *,SEL,CharStringLessmap_sels;
[snip]
That''s your first problem. That will base look up on pointer value, not
string value. Instead, you want:
#include <cstring>
struct CharStringLess
{
bool operator()(char const* lhs, char const* rhs) const
{
return std::strcmp(lhs, rhs) < 0;
}
};
static std::map<const char*, SEL, CharStringLessmap_sels;
[snip]
std :: map< SEL,IMP * class_imps; //这里我得到了有问题的地图*
std::map<SEL, IMP* class_imps; // here I get the problematic "map *"
[snip]
[snip]
class_imps [sel] = imp; //不能用''map *''来做,那我应该是什么?b $ b我应该这么做?
class_imps[sel] = imp; // can''t do that with ''map*'', what
should I so?
你只需要取消引用指针:
(* class_imps)[sel] = imp;
或者您可以使用参考:
std :: map< SEL,IMP>& class_imps_ref = * class_imps;
class_imps_ref [sel] = imp;
You just need to dereference the pointer:
(*class_imps)[sel] = imp;
Or you could use a reference:
std::map<SEL, IMP>& class_imps_ref = *class_imps;
class_imps_ref[sel] = imp;
}
else
{
imp = class_imps [sel]; //不能用''地图*''那样做,应该是什么?b $ b我是这样的?
}
else
{
imp = class_imps[sel]; // can''t do that with ''map*'', what should
I so?
与上述相同。
Tom
Same as above.
Tom
这篇关于STL问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!