Advice on a better way to extend C++ STL container with user-defined methods

后端 未结 8 1568
星月不相逢
星月不相逢 2020-12-01 08:41

I inherited from C++ STL container and add my own methods to it. The rationale was such that to the clients, it will look act a regular list, yet has application-specific me

8条回答
  •  生来不讨喜
    2020-12-01 09:10

    I can't see why you need to extend vector with those methods. You could just write them as standalone functions, eg:

    int MaxA(const std::vector& vec) {
        if(vec.empty()) {
            throw;
        }
    
        int maxA = vec[0].a;
        for(std::vector::const_iterator i = vec.begin(); i != vec.end(); ++i) {
            if(i->a > maxA) {
                maxA = i->a;
            }
        }
        return maxA;
    }
    

    Or there's std::max_element which would do much the same... minus the throwing of course.

提交回复
热议问题