Skip to content

Instantly share code, notes, and snippets.

@Teebor-Choka
Created July 21, 2019 20:52
Show Gist options
  • Save Teebor-Choka/beeb8b13ac877ea4bc02565357a4e34b to your computer and use it in GitHub Desktop.
Save Teebor-Choka/beeb8b13ac877ea4bc02565357a4e34b to your computer and use it in GitHub Desktop.
Programming tricks - Sentinel
/**
* 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