gpt4 book ai didi

c++ - multimap 清除

转载 作者:行者123 更新时间:2023-11-28 08:08:03 26 4
gpt4 key购买 nike

这是我使用多 map 制作的一个简单的事件系统;当我使用 CEvents::Add(..) 方法时,它应该插入并进入多重映射。问题是,当我触发这些事件时, multimap 似乎是空的。我确定我没有调用删除方法 [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();
}
}

最佳答案

它不应该是一个代码审查网站,但我无法自拔......

// "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) 的唯一性自动配对...尽管分派(dispatch)事件的代码会稍微复杂一些,所以不确定它是否会成功(代码方面或性能方面)。

关于c++ - multimap 清除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9844058/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com