fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <array>
  4. #include <cstring>
  5. #include <cassert>
  6.  
  7. template<class e, class t, int N>
  8. std::basic_istream<e,t>& operator>>(std::basic_istream<e,t>& in, const e(&str_lit)[N]) {
  9. std::array<e, N-1> buffer; //get buffer
  10. in >> std::ws; //skips whitespace
  11. if (N)
  12. in.read(&buffer[0], N-1); //read the rest
  13. if (strncmp(&buffer[0], str_lit, N-1)) //if it failed
  14. in.setstate(in.rdstate() | std::ios::badbit); //set the state
  15. return in;
  16. }
  17. template<class e, class t>
  18. std::basic_istream<e,t>& operator>>(std::basic_istream<e,t>& in, const e& char_lit) {
  19. e buffer; //get buffer
  20. in >> buffer; //read data
  21. if (buffer != char_lit) //if it failed
  22. in.setstate(in.rdstate() | std::ios::badbit); //set the state
  23. return in;
  24. }
  25. //redirect mutable char arrays to their normal function
  26. template<class e, class t, int N>
  27. std::basic_istream<e,t>& operator>>(std::basic_istream<e,t>& in, e(&carray)[N]) {
  28. return std::operator>>(in, carray);
  29. }
  30.  
  31. int main()
  32. {
  33. std::string s;
  34. if (std::cin >> '(' >> s >> ')')
  35. std::cout << "SUCCEED";
  36. else
  37. std::cout << "FAIL";
  38. }
Success #stdin #stdout 0s 3020KB
stdin
(foo)
stdout
FAIL