Menu

useStlAlgorithm not suitable suggestion

3 days ago
3 days ago
  • Nikita Leontiev

    Nikita Leontiev - 3 days ago

    cppcheck 2.18.0 suggests using std::find_if where it can't be simply applied:

    #include <vector>
    
    bool get_index(const std::vector<int> & items, int value, size_t & index)
    {
        size_t current_index = 0;
        size_t count = items.size();
        for (size_t i = 0; i < count; ++i)
        {
            if (items[i] == value)
            {
                if (current_index != index)
                {
                    current_index++;
                    continue;
                }
                index = i;
                return true;
            }
        }
        return false;
    }
    
    int main()
    {
        std::vector<int> items;
        items.push_back(0);
        items.push_back(1);
        items.push_back(1);
        items.push_back(1);
        size_t index = 2;
        get_index(items, 1, index);
        return 0;
    }
    
    test\main.cpp:10:3: style: Consider using std::find_if algorithm instead of a raw loop. [useStlAlgorithm]
      {
      ^
    
     
  • CHR

    CHR - 3 days ago

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/14047

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.