Advertisement
Guest User

Untitled

a guest
Sep 17th, 2014
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <boost/config/warning_disable.hpp>
  2. #include <boost/spirit/include/qi.hpp>
  3.  
  4. #include <iostream>
  5. #include <string>
  6.  
  7. namespace qi = boost::spirit::qi;
  8. namespace ascii = boost::spirit::ascii;
  9.  
  10. void onDouble(const double& d) {
  11.     std::cout << "Found: " << d << std::endl;
  12. }
  13.  
  14. template <typename Iterator>
  15. bool parse_numbers(Iterator first, Iterator last) {
  16.     using qi::double_;
  17.     using qi::phrase_parse;
  18.     using ascii::space;
  19.     bool r = phrase_parse(
  20.         first,
  21.         last,
  22.         double_[&onDouble] >> *(',' >> double_[&onDouble]),
  23.         space
  24.     );
  25.     if (first != last) {
  26.         return false;
  27.     }
  28.     return r;
  29. }
  30.  
  31. int main() {
  32.     std::string data("21.2, 34,5, 58.4, 56");
  33.     if (parse_numbers(data.begin(), data.end())) {
  34.         std::cout << "It works" << std::endl;
  35.     } else {
  36.         std::cout << "It failed" << std::endl;
  37.     }
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement