Created
July 21, 2019 20:52
-
-
Save Teebor-Choka/beeb8b13ac877ea4bc02565357a4e34b to your computer and use it in GitHub Desktop.
Programming tricks - Sentinel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Sentinels: http://stackoverflow.com/a/5384593 | |
| */ | |
| template<typename T> | |
| decltype(auto) find_without_sentinel(const T& c) | |
| { | |
| for (iterator i=c.begin(); i!=c.end(); ++i) // first branch here | |
| { | |
| if (*i == x) // second branch here | |
| return i; | |
| } | |
| return list.end(); | |
| } | |
| template<typename T> | |
| decltype(auto) find_using_sentinel(const T& c) | |
| { | |
| iterator i=list.begin(); | |
| *list.end() = x; | |
| while (*i != x) // just this branch! | |
| ++i; | |
| return i; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment