Multimap clears
Multimap clears
下面是我使用多映射创建的一个简单事件系统;当我使用CEvents::Add(..)方法时,它应该插入并进入多映射。问题是,当我触发这些事件时,多映射似乎是空的。我确信我没有调用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();
}
}
它不应该是一个代码审查网站,但我忍不住。。。
// "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)
对的唯一性。。。不过,调度事件的代码会稍微复杂一些,所以不确定它是否会成功(代码方面或性能方面)。
这不是如何使用映射。您可以find( key )
,这可能比迭代集合中的所有元素更快。如果您想确保密钥是唯一的,那么您可以使用普通映射,而不是多映射,后者明确用于将重复密钥存储为唯一实体。
编辑:根据您的评论更新密钥不应该是唯一的。然后应该使用lower_bound
和upper_bound
进行搜索,这比检查所有键要好,因为find
(来自内存)只返回lower_bound
。或者,您可以迭代equal range
的结果(正如Mark在评论中建议的那样),这将导致相同的结果。
只要浏览一下您的代码(由于缺少erase
、clear
或swap
调用),我想您的问题是将元素添加到集合中,而不是将它们"清空"。
顺便说一句,为了让它更快,当你找到项目时,最好打破你的for:
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成员函数返回一个迭代器,该迭代器指向新元素插入多映射的位置。"(msdn)
为了确保插入正确,请定制insert的返回值。
好的,终于解决了;供将来参考:
实际情况是,每次声明实例时,都会重新定义多映射。解决方案是创建一个指向类的全局指针。
感谢所有回答的人!
- 使用std::multimap迭代器创建std::list
- std::multimap<std::chrono::milliseconds, T>::rbegin 在 MSVS-13 中指向 end()?
- C++在使用std::multimap时出现运行时错误的几率很小
- 用 C++20 的给定键读取/写入 std::multimap 的所有值?
- std::multimap::erase() 在迭代时
- 访问 std::multimap 值的最佳方式?
- QPointer::clear() 是删除其引用的指针,还是"Clears this QPointer object."其他含义?
- MAP和Multimap之间的C 模板专业化
- 如何使用 std::multimap 将整数键映射到两个用作多维数组坐标的整数值(对于井字)?
- "std::multimap"是否保证每个键的实际值在相等范围内?
- 无法插入qcustomplot :: qcpgraph中的Multimap C
- 特定键在 std::multimap 中出现的次数
- swig:std/multimap.i似乎打破了TCL包装
- 如何使用"std::multimap"或任何其他容器对多个值进行排序?
- c++ 为什么 std::multimap 比 std::p riority_queue 慢
- 检查multimap c++中是否已经存在值
- std :: multimap :: emplace覆盖旧键
- 初始化静态std :: multimap在其中声明的方法
- ISO C 禁止没有类型的“ Multimap”声明
- Multimap clears