替换' find_if '函数
Substituting `find_if` function
我使用STL find_if
编写了一个类方法。代码如下:
void
Simulator::CommunicateEvent (pEvent e)
{
pwEvent we (e);
std::list<pEvent> l;
for (uint32_t i = 0; i < m_simulatorObjects.size (); i++)
{
l = m_simulatorObjects[i]->ProcessEvent (we);
// no action needed if list is empty
if (l.empty ())
continue;
// sorting needed if list comprises 2+ events
if (l.size () != 1)
l.sort (Event::Compare);
std::list<pEvent>::iterator it = m_eventList.begin ();
std::list<pEvent>::iterator jt;
for (std::list<pEvent>::iterator returnedElementIt = l.begin ();
returnedElementIt != l.end ();
returnedElementIt++)
{
// loop through the array until you find an element whose time is just
// greater than the time of the element we want to insert
Simulator::m_eventTime = (*returnedElementIt)->GetTime ();
jt = find_if (it,
m_eventList.end (),
IsJustGreater);
m_eventList.insert (jt, *returnedElementIt);
it = jt;
}
}
}
不幸的是,我后来发现将运行这些代码的机器配备了libstdc++库版本4.1.1-21,显然缺少find_if
。不用说,我不能升级这个库,也不能请别人来做。
编译时,我得到的错误是:
simulator.cc: In member function ‘void sim::Simulator::CommunicateEvent(sim::pEvent)’:
simulator.cc:168: error: no matching function for call to ‘find_if(std::_List_iterator<boost::shared_ptr<sim::Event> >&, std::_List_iterator<boost::shared_ptr<sim::Event> >, sim::Simulator::<anonymous struct>&)’
simulator.cc: In static member function ‘static void sim::Simulator::InsertEvent(sim::pEvent)’:
simulator.cc:191: error: no matching function for call to ‘find_if(std::_List_iterator<boost::shared_ptr<sim::Event> >&, std::_List_iterator<boost::shared_ptr<sim::Event> >, sim::Simulator::<anonymous struct>&)’
make: *** [simulator.o] Error 1
我该如何解决这个问题?
我想我可以像这里描述的那样定义一个find_if
函数。然而,我有一些担忧:
- 性能怎么样?使用find_if的函数需要尽可能高效。
- 我如何做条件编译?我找不到告诉libstdc++安装版本的宏。
你对此有什么看法?蒂雅,Jir
<标题>引用源文件:simulator.h和simulator.cc
<标题> 解决方案在Simulator
类之外定义IsJustGreater
并声明IsJustGreater_s
为Simulator
的友元:
struct IsJustGreater_s : public std::unary_function<const pEvent, bool> {
inline bool operator() (const pEvent e1) {return (e1->GetTime () > Simulator::m_eventTime);}
} IsJustGreater;
在find_if
中这样调用IsJustGreater
:jt = find_if (it, m_eventList。end (), sim::IsJustGreater);
从错误中可以看出,您试图使用匿名类型作为参数。我不认为匿名类型可以作为模板参数。
从错误中,我相信你有这样的内容:
class Simulator {
struct {
bool operator(const pEvent& p) { ... } ;
} IsJustGreater;
}
你想要的是给它一个名字,然后改变find_if来实例化这个类(见下文)
class Simulator {
// class is now an inner named-class
struct IsJustGreater {
bool operator(const pEvent& p) { ... } ;
};
}
// This is how you use the class
jt = std::find_if(it, m_eventList.end(), IsJustGreater() );
我看到你在std::list
之前使用std::
限定符,而不是std::find_if
。尝试将std::
放在前面,以便编译器可以在命名空间中找到它。
相关文章:
- Insert函数不适用于2 if语句C++
- Fmod 函数清楚地输出一个预期的双精度值,但 if(fmod == 预期的双精度值)的计算结果不是 true
- 调用一个小函数两次(例如在if条件和主体中)比将结果存储在局部变量中更可取
- 在 if 语句中调用重载构造函数失败
- 为什么当 while 循环中的 if 条件变为 false 时,我的函数不输出最后一条语句?
- 简化 if 流函数的 while 循环中的大量 while 循环
- 在 constexpr-if 条件下比较 constexpr 函数参数会导致错误
- if-else 与三元函数调用性能
- 函数如何在不这样做的情况下在新线程上运行"as if"?
- 无法编译包含"if constexpr"的函数模板实例化
- 性能:否则如果 vs if 在已经返回的函数中
- 重构模板函数中的常量表达式 if 语句
- if, else if, else 函数未准确显示结果
- 为什么 if 语句对于运算符重载函数计算 false
- 使用 if/else if 的函数输出问题
- 'if constexpr branch'不会在模板函数内的 lambda 中被丢弃
- C++创建一个类似于 while、if 或 for 的函数
- 如何正确使用这个函数的if语句?
- 函数'If'的隐式声明
- 使用模板检查结构体中的字段,启用函数if,并在失败时给出一个漂亮的错误消息