Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3.  
  4. namespace detail {
  5.  
  6. template<char...Chars>
  7. bool checkSize(const std::string& s) {
  8.     return sizeof...(Chars) <= s.size();;
  9. }
  10.  
  11. template<size_t N, char C, char...Chars>
  12. struct StartsWith {
  13.     static const bool apply(const std::string& s) {
  14.         return s[N] == C && StartsWith<N + 1, Chars...>::apply(s);
  15.     }
  16. };
  17.  
  18. template<size_t N, char C>
  19. struct StartsWith<N, C> {
  20.     static const bool apply(const std::string& s) {
  21.         return s[N] == C;
  22.     }
  23. };
  24.  
  25. } // detail
  26.  
  27. template<char...Chars>
  28. bool startsWith(const std::string& s) {
  29.     return detail::checkSize<Chars...>(s) && detail::StartsWith<0, Chars...>::apply(s);
  30. }
  31.  
  32. int main() {
  33.     std::string x = "zlonejzlejsi";
  34.     std::cout << startsWith<'z','l','o'>(x) << std::endl;
  35.  
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement