fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <experimental/optional>
  4.  
  5. std::experimental::optional<int> find_int_strtol( const std::string & s )
  6. {
  7. for( const char *p = s.c_str(); *p != '\0'; p++ )
  8. {
  9. char *next;
  10. int value = std::strtol( p, &next, 10 );
  11. if( next != p ) {
  12. return value;
  13. }
  14. }
  15. return {};
  16. }
  17.  
  18. int main()
  19. {
  20. for( std::string line; std::getline( std::cin, line ); )
  21. {
  22. auto n = find_int_strtol( line );
  23. if( n )
  24. {
  25. std::cout << "Got " << n.value() << " in " << line << std::endl;
  26. }
  27. }
  28. return 0;
  29. }
  30.  
Success #stdin #stdout 0s 15240KB
stdin
hello 123-abc
cruel
world 456-def
stdout
Got 123 in hello 123-abc
Got 456 in world 456-def