Does std::find on empty vector cause undefined behaviour?

  • A+
Category:Languages

trying to find an info on what happens if an empty vector used during std::find but didn't had much luck finding any info.

My question is, if an empty vector passed to std::find, is the return value always a nullptr or is it undefined behaviour?

  std::vector<int> someDataContainer;   auto it = std::find(someDataContainer.begin(), someDataContainer.end(), 1); 

 


The return value of find when the element is not present is the end iterator:

[alg.find] (emphasis mine):

Let E be:

  • *i == value for find,
  • [...]

Returns: The first iterator i in the range [first, last) for which E is true. Returns last if no such iterator is found.

This includes the element not being present because the range is empty.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: