Testing whether an iterator points to the last item?

前端 未结 9 893
旧巷少年郎
旧巷少年郎 2020-12-23 11:31

I have an stl iterator resulting from a std::find() and wish to test whether it is the last element. One way to write this is as follows:

mine *match = some         


        
9条回答
  •  时光说笑
    2020-12-23 12:32

    Here's another potential solution:

    template bool is_last(Iterator it, const Container& cont)
    {
        // REQUIREMENTS:
        // the iterator must be a valid iterator for `cont`
        if( it == cont.end() )
            return false;   // or throw if you prefer
        return (++it) == cont.end();
    }
    

提交回复
热议问题