fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5. #include <iterator>
  6. #include <sstream>
  7.  
  8. std::vector<std::string> SplitInputString(const std::string& input)
  9. {
  10. std::vector<std::string> tokens;
  11.  
  12. std::istringstream ss(input);
  13. std::copy(std::istream_iterator<std::string>(ss),
  14. std::istream_iterator<std::string>(),
  15. std::back_inserter(tokens));
  16.  
  17. return tokens;
  18. }
  19.  
  20. struct ParsedInputData
  21. {
  22. ParsedInputData(const std::string& command, const std::vector<std::string>& variables)
  23. : command(command)
  24. , variables(variables)
  25. {
  26.  
  27. }
  28.  
  29. std::string command;
  30. std::vector<std::string> variables;
  31. };
  32.  
  33. int main(int argc, const char* argv[])
  34. {
  35. std::vector<std::string> testInputs {"add A B", "add B F", "breadth F", "subtract A B"};
  36.  
  37. std::vector<ParsedInputData> parsedInputDatas;
  38.  
  39. for (const auto& input : testInputs)
  40. {
  41. const auto& tokens = SplitInputString(input);
  42. auto parsedInputData = ParsedInputData(tokens.front(), std::vector<std::string>(tokens.begin() + 1, tokens.end()));
  43. parsedInputDatas.push_back(parsedInputData);
  44. }
  45.  
  46. for (const auto& parsedInputData : parsedInputDatas)
  47. {
  48. std::cout << "Command is: " << parsedInputData.command << std::endl;
  49. std::cout << "Variables: ";
  50. for(const auto& variable : parsedInputData.variables)
  51. {
  52. std::cout << variable << " ";
  53. }
  54.  
  55. std::cout << std::endl;
  56. }
  57.  
  58. return 0;
  59. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
Command is: add
Variables: A B 
Command is: add
Variables: B F 
Command is: breadth
Variables: F 
Command is: subtract
Variables: A B