Is it possible to use std :: max_element () for std :: deque in C ++ 11?

Can I code as std::max_element(std::begin(my_deque), std::end(my_deque)) ?

I ask because I know that deque is not guaranteed to be stored continuously, so I want to know if it will behave correctly when using functions with an iterator, like std::max_element ?

Thank you very much!

+5
source share
2 answers

std::max_element has a signature in the form

 template<class ForwardIterator> ForwardIterator max_element(ForwardIterator first, ForwardIterator last); 

From the name of the template type, we know that it needs a forwarding iterator. Per [container.requirements.general] -Table 96 we know that std::deque uses

any iterator category that meets the requirements of the direct iterator

So, since it uses an advanced iterator or better, it will always be ok.

+7
source

Yes, it will work correctly. The overload of std::max_element to be called in this case is

 template< class ForwardIt > ForwardIt max_element(ForwardIt first, ForwardIt last); 

The only requirements for iterators are

first , last - forward iterators defining the range to study

Thus, there are no requirements for random access iterators, but only for iterators.

+6
source

All Articles