I suppose you may need to use that finding function not once.
(我想您可能不需要一次使用该查找功能。)
So, according to DRY, you need to separate the block where you invoke an std::find_if algorithm to a distinct wrapper function. (因此,根据DRY,您需要将调用std :: find_if算法的块分隔为不同的包装函数。)
{
// ... function beginning
auto found = std::find_if(m_programs.cbegin(), m_programs.cend(),
[&](const auto& prog)
{
bool b = prog->compare(...);
if (b)
TraceInfo(...);
return b;
});
if (found == m_programs.cend())
return nullptr;
return *found;
}
The suggestion is good.
(这个建议很好。)
An STL algorithm migth be able to choose an appropriate approach based on your container type. (STL算法可以根据您的容器类型选择合适的方法。)
Furthermore, I suggest you to use a self-balancing container like an std::set.
(此外,我建议您使用类似std :: set的自平衡容器。)
// I don't know what kind of a pointer you use.
using pProgType = std::shared_pointer<ProgType>;
bool compare_progs(const pProgType &a, const pProgType &b)
{
return std::less(*a, *b);
}
std::set<std::shared_pointer<prog_type>,
std::integral_constant<decltype(&compare_progs), &compare_progs>> progs.
This is a sorted container, so you will spend less time for searching a program by a value, given you implement a compare operator (which is invoked by std::less).
(这是一个排序的容器,因此,在实现比较运算符(由std :: less调用)的情况下,您将花费较少的时间来按值搜索程序。)
If you can use an stl function, use it.
(如果可以使用stl函数,请使用它。)
This way you will not have to remember what you invented, because stl is properly documented and safe to use. (这样,您将不必记住自己的发明,因为stl已正确记录在案并且可以安全使用。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…