Skip to content

Return value of thrust::minmax_element differs from STL's minmax_element #699

Open

Description

@maribu

In both STL and thrust a call to minmax_element returns a pair of iterators, the first points to the minimum and the second to the maximum value. In both STL and thrust, if more than run minimum exists, the first value is chosen. If more than one maximum value exists, the STL version chooses the last candidate while thrust chooses the first one. While I think the STL is quite inconsistent here, I'd still prefer if thrust would behave just like the STL

Best wishes,
maribu

Activity

  1. self-assigned this
    on Sep 17, 2017
  2. added
    thrust
    on Feb 22, 2023
  3. changed the title [-]Return value of minmax_element differs form STL's minmax_element[/-] [+]Return value of minmax_element differs from STL's minmax_element[/+] on Mar 7, 2023
  4. jrhemstad commented on Mar 7, 2023

    @jrhemstad
    Collaborator

    Confirmed that this difference in behavior still exists: https://godbolt.org/z/dqzxKEYzM

    thrust::minmax_element will select the first maximum element whereas std::minmax_element picks the last.

    fwiw, std::minmax_element and std::max_element are frustratingly inconsistent:

    std::minmax_element:

    If several elements are equivalent to the largest element, the iterator to the last such element is returned.

    std::max_element:

    If several elements in the range are equivalent to the greatest element, returns the iterator to the first such element.

    In contrast, Thrust is self-consistent between thrust::minmax_element and thrust::max_element:

    thrust::minmax_element

    It returns a pair of iterators (imin, imax) where imin is the same iterator returned by min_element and imax is the same iterator returned by max_element.

    While I agree it would be good for Thrust to be consistent with std:: here, this would be a breaking change and one that is changing to arguably worse behavior... I'll keep the issue open for now, but I don't expect this is something we'll be doing anytime soon.

  5. maribu commented on Mar 7, 2023

    @maribu
    Author

    Thx for looking into this. I worked back in 2016 when I was still a student with thrust, but I haven't looked much into it after finishing that particular course.

    Ebedded C programming and IoT is now the domain I'm most active in. I have to admit that by now I have even forgotten to ever file issues here :-(

  6. transferred this issue fromNVIDIA/thruston Nov 8, 2023
  7. moved this to Todo in CCCLon Jan 17, 2026
  8. changed the title [-]Return value of minmax_element differs from STL's minmax_element[/-] [+]Return value of thrust::minmax_element differs from STL's minmax_element[/+] on Aug 9, 2026
  9. moved this from Todo to In Progress in CCCLon Aug 9, 2026
  10. moved this from In Progress to In Review in CCCLon Aug 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

thrust

Type

No type

Projects

  • Status
    In Review

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions