Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8540383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:35:07+00:00 2026-06-11T11:35:07+00:00

The C++ standard library’s max_element algorithm requires the iterators passed as inputs to model

  • 0

The C++ standard library’s max_element algorithm requires the iterators passed as inputs to model ForwardIterator.

My understanding is that ForwardIterator refines InputIterator by specifying that you can use a ForwardIterator to iterate over the same range multiple times. Therefore, multi-pass algorithms require ForwardIterators.

However, max_element is not a multi-pass algorithm – it is sufficient to iterate over a range once to determine its maximum element. So why does max_element need the additional capabilities of ForwardIterator?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:35:08+00:00Added an answer on June 11, 2026 at 11:35 am

    std::max_element returns an iterator to the maximum element. If you provide a single pass range, that iterator will not be valid any more, since the algorithm has to perform a full pass on the range.

    On a single pass range, you cannot keep an usable iterator to a previous value. This is due to a post-condition on ++r given in Table 107 in the standard:

    post: any copies of the previous value of r are no longer required either to be dereferenceable or to be in the domain of ==.

    Basically, a single pass range is a range that “disappears” as you pass through it, and std::max_element needs a range that sticks around in order to return an iterator to (possibly) the middle of it.

    One could write an algorithm to compute the maximum that returned the actual maximum value instead of an iterator, but that would require the values to be copyable in order to return it by value. Movable would not be enough as moving would prevent the use of const iterators. And returning by reference would not be an option either, as that would mean the range actually stuck around.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an algorithm in the standard library that can add a value to
The C++11 standard library includes the following related algorithms: template <class InputIterator, class ForwardIterator>
In standard library, I found that namespace std is declared as a macro. #define
With the C standard library stdio.h , I read that to output ASCII/text data,
The OCaml standard library has a wonderful Set implementation that uses a very efficient
In Java's standard library, is there a method that would allow one to sort
We (should) know that C++ standard library containers, including std::string , are not meant
Why does C++ standard library work with a comparison function that is strict weak
So the standard c++ library mainly contains roughly 7 categories, what's the rationale/prototype that
I need an algorithm or a standard library function for comparing two vector elements,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.