这是我使用多重映射制作的简单事件系统;当我使用CEvents :: Add(..)方法时,它应该插入并输入到multimap中。问题是,当我触发这些事件时,多图显示为空。我确定我没有调用delete方法[CEvents :: Remove]。这是代码:
//Code:
..
CEvents Ev;
Ev.Add("onButtonBReleased",OutputFST);
..
// "CEvents.h"
class CEvents
{
public:
void Add ( string EventName, void(*fn)(void));
void Remove ( string EventName, void(*fn)(void));
void Trigger ( string EventName );
//protected:
bool Found;
std::multimap<string,void(*)(void)> EventsMap;
std::multimap<string,void(*)(void)>::iterator EvMapIt;
};
//CEvents.cpp
void CEvents::Add (string EventName, void (*fn)(void))
{
if (!EventsMap.empty())
{
Found = false;
for (EvMapIt = EventsMap.begin(); EvMapIt != EventsMap.end(); EvMapIt++)
{
if ((EvMapIt->first == EventName) && (EvMapIt->second == fn))
{
CTools tools;
tools.ErrorOut("Function already bound to same event... Not registering event");
Found = true;
}
}
if (!Found)
{
EventsMap.insert(std::pair<string,void(*)(void)>(EventName,fn));
std::cout<<"Added, with size "<<(int) EventsMap.size()<<std::endl; //Getting 1
}
}
else
{
EventsMap.insert (std::pair<string,void(*)(void)>(EventName,fn));
std::cout<<"Added, with size "<<(int) EventsMap.size()<<std::endl; //Getting 1
}
}
void CEvents::Trigger (string EventName)
{
std::cout<<"Triggering init"<<std::endl;
std::cout<<(int) EventsMap.size()<<std::endl; //Getting 0
for (EvMapIt = EventsMap.begin(); EvMapIt != EventsMap.end(); EvMapIt++)
{
std::cout<<"Triggering proc"<<std::endl;
if (EvMapIt->first == EventName)
EvMapIt->second();
}
}
答案 0 :(得分:1)
它不应该是代码审查网站,但我无法帮助自己......
// "CEvents.h"
class CEvents
{
public:
typedef void (*Callback)(void);
// 1. Don't use `using namespace` in header files
// 2. Pass by const reference to avoid a copy
// 3. Function Pointers are easier to deal with when typedef'd
void Add(std::string const& EventName, Callback fn);
void Remove(std::string const& EventName, Callback fn);
void Trigger(std::string const& EventName);
// Attributes should be `private` or `public`, `protected` is for functions.
// If you read otherwise, consider how this violates encapsulation.
//protected:
private: // cause nobody's touching my stuff lest they break it!
// useless in this class, should be local variables in the routines
// bool Found;
// MapType::iterator EvMapIt;
// typedef make life easier, spelling that out each time is just tiring.
typedef std::multimap<std::string, Callback> MapType;
MapType EventsMap;
};
好的,让我们去找源文件。
//CEvents.cpp
// Whole rewrite to use idiomatic interfaces
void CEvents::Add(std::string const& EventName, Callback fn)
{
// Retrieve the range of callbacks registered for "EventName"
std::pair<MapType::iterator, MapType::iterator> const range =
EventsMap.equal_range(EventName);
// Check that this callback is not already registered.
for (MapType::iterator it = range.first, end = range.second;
it != end; ++it)
{
if (it->second == fn) {
// Are you sure `ErrorOut` should not be a free function
// or at least a `static` function ?
// It feels weird instantiating this class.
CTools tools;
tools.ErrorOut("Function already bound to same event..."
" Not registering event");
// If it is in there, nothing to do, so let's stop.
return;
}
}
// If we are here, then we need to add it.
// Let's give a hint for insertion, while we are at it.
EventsMap.insert(range.second, std::make_pair(EventName, fn));
// the (int) cast was C-like (bah...) and unnecessary anyway
std::cout << "Added, with size " << EventsMap.size() << std::endl;
}
void CEvents::Trigger (std::string const& EventName)
{
std::cout << "Triggering init" << std::endl;
std::cout << EventsMap.size() << std::endl; //Getting 0
// Retrieve the range of callbacks registered for `EventName`
std::pair<MapType::const_iterator, MapType::const_terator> const range =
EventsMap.equal_range(EventName);
// Call each callback in turn
for (MapType::const_iterator it = range.first, end = range.second;
it != end; ++it)
{
it->second();
}
}
当然,它可能无法解决您的问题,但它比它应该缩小范围要短得多。
当然,使用std::set<std::pair<std::string, Callback>>
可能更简单,因为它可以自动确保(EventName, fn)
对的单一性......发送事件的代码会稍微复杂一些,所以不会确定这将是一场胜利(代码明智或表现明智)。
答案 1 :(得分:0)
这不是如何使用地图。您可以find( key )
,这可能比迭代集合中的所有元素更快。如果您想确保密钥是唯一的,那么您可以使用普通的地图而不是多地图,这明确地用于将重复密钥存储为唯一实体。
编辑:根据您的评论更新密钥不应该是唯一的。然后,您应该使用lower_bound
和upper_bound
进行搜索,这比检查所有密钥更好,因为find
(来自内存)只返回lower_bound
。或者,您可以迭代equal range
的结果(在评论中建议的标记),这将导致相同的结果。
只是看一下你的代码(以及缺少erase
,clear
或swap
次调用)我猜你的问题是当你将你的元素添加到集合时而不是他们被'清空'。
答案 2 :(得分:0)
顺便说一句,为了让它更快,最好在你找到物品时打破你的行为:
for (EvMapIt = EventsMap.begin(); EvMapIt != EventsMap.end(); EvMapIt++)
{
if ((EvMapIt->first == EventName) && (EvMapIt->second == fn))
{
CTools tools;
tools.ErrorOut("Function already bound to same event... Not registering event");
Found = true;
break;
}
}
修改强> “insert成员函数返回一个迭代器,指向新元素插入到multimap中的位置。” (MSDN)
确保向右插入,cout返回插入值。
答案 3 :(得分:0)
好的,终于解决了;供将来参考:
每次声明实例时,都会重新定义多重映射。 解决方案是创建一个指向该类的全局指针。
感谢所有回答的人!