Skip to content

Instantly share code, notes, and snippets.

@root42
Last active August 25, 2023 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save root42/4b2885d45c82b34462327bc911183a60 to your computer and use it in GitHub Desktop.
Save root42/4b2885d45c82b34462327bc911183a60 to your computer and use it in GitHub Desktop.
C++ string last_index_of similar to Java lastIndexOf
template<typename T>
typename T::size_type
last_index_of(const T &s, const T &q)
{
typename T::size_type
pos = std::string::npos,
res = std::string::npos;
while( (pos = s.find(q, (pos == std::string::npos ? 0 : pos + q.size()))) != T::npos) {
res = pos;
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment