fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <vector>
  4. #include <utility>
  5.  
  6. typedef std::pair<int, int> point;
  7. typedef std::vector<point> points;
  8.  
  9. std::istream& operator>>(std::istream &in, point &out)
  10. {
  11. char ch1, ch2, ch3;
  12. if (in >> ch1 >> out.first >> ch2 >> out.second >> ch3)
  13. {
  14. if ((ch1 != '(') || (ch2 != ',') || (ch3 != ')'))
  15. in.setstate(std::ios_base::failbit);
  16. }
  17. return in;
  18. }
  19.  
  20. std::istream& operator>>(std::istream &in, points &out)
  21. {
  22. point pt;
  23. char ch;
  24.  
  25. if (!(in >> ch))
  26. return in;
  27.  
  28. if (ch != '[')
  29. {
  30. in.setstate(std::ios_base::failbit);
  31. return in;
  32. }
  33.  
  34. ch = in.peek();
  35. do
  36. {
  37. if (ch == std::istream::traits_type::eof())
  38. {
  39. in.setstate(std::ios_base::failbit);
  40. break;
  41. }
  42.  
  43. if (ch == ']')
  44. {
  45. in.ignore(1);
  46. break;
  47. }
  48.  
  49. if (ch != '(')
  50. {
  51. in.setstate(std::ios_base::failbit);
  52. break;
  53. }
  54.  
  55. if (!(in >> pt))
  56. break;
  57.  
  58. out.push_back(pt);
  59.  
  60. ch = in.peek();
  61. if (ch == ',')
  62. {
  63. in.ignore(1);
  64. ch = in.peek();
  65. }
  66. }
  67. while (true);
  68.  
  69. return in;
  70. }
  71.  
  72. int main() {
  73. std::istringstream iss("[(1,2),(10,4),(5,12)]");
  74. points coords;
  75. iss >> coords;
  76. for(auto &pt: coords)
  77. std::cout << pt.first << ' ' << pt.second << std::endl;
  78. return 0;
  79. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
1 2
10 4
5 12