Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
689 views
in Technique[技术] by (71.8m points)

c++ - C ++ CppCheck算法建议(std :: find_if而不是原始循环)的相关性(C++ CppCheck algorithm suggestion (std::find_if instead of raw loop) pertinence)

CppCheck suggest me to replace one of my code by a STL algorithm, I'm not against it, but I don't know how to replace it.

(CppCheck建议我用STL算法替换我的代码之一,我并不反对,但是我不知道如何替换它。)

I'm prettry sure this is a bad suggestion (There is warning about experimental functionalities in CppCheck).

(我很确定这不是一个好建议(CppCheck中有关于实验功能的警告)。)

Here is the code :

(这是代码:)

    /* Cutted beginning of the function ... */

    for ( const auto & program : m_programs )
    {
        if ( program->compare(vertexShader, tesselationControlShader, tesselationEvaluationShader, geometryShader, fragmentShader) )
        {
            TraceInfo(Classname, "A program has been found matching every shaders.");

            return program;
        }
    }

    return nullptr;
} /* End of the function */

And near the if condition I got : "Consider using std::find_if algorithm instead of a raw loop."

(在if条件附近,我得到:“考虑使用std :: find_if算法而不是原始循环。”)

I tried to use it, but I can't get the return working anymore... Should I ignore this suggestion ?

(我尝试使用它,但无法继续使用退货...我应该忽略这个建议吗?)

  ask by Sébastien Bémelmans translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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已正确记录在案并且可以安全使用。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...