Why does the standard library have find and find_if?

前端 未结 4 938
挽巷
挽巷 2021-01-11 16:06

Couldn\'t find_if just be an overload of find? That\'s how std::binary_search and friends do it...

4条回答
  •  广开言路
    2021-01-11 16:47

    You can certainly implement find in terms of find_if using some sort of equality predicate.

    I would guess that the real reason is that you can implement find fairly easily and provide performant specialised implementations for typical encountered types; if you are using find_if, the predicate you pass in can be arbitrarily complex, which gives the library implementer less scope of optimisation.

    Also, C++ has the philosphy of "you don't pay for what you don't use" and you'd normally expect that you don't want to pay for a predicate evaluation if a simple comparison will do.

提交回复
热议问题