fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <stdexcept>
  5. #include <vector>
  6.  
  7. std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems)
  8. {
  9. std::stringstream ss(s);
  10. std::string item;
  11. while (std::getline(ss, item, delim))
  12. {
  13. elems.push_back(item);
  14. }
  15. return elems;
  16. }
  17.  
  18. std::vector<std::string> split(const std::string &s, char delim)
  19. {
  20. std::vector<std::string> elems;
  21. split(s, delim, elems);
  22. return elems;
  23. }
  24.  
  25. int main()
  26. {
  27. try
  28. {
  29. //Beispiel...
  30. std::string receivedPacket = "LOGIN|1001|12345678|4.1";
  31.  
  32. auto packetSplitted = split(receivedPacket, '|');
  33.  
  34. auto userId = packetSplitted.at(1);
  35. std::cout << "UserID: " << userId;
  36. }
  37. catch(const std::out_of_range &rangeException)
  38. {
  39. std::cout << "Oh Oh... Out of range :(" << std::endl;
  40. }
  41. catch(const std::exception &unkException)
  42. {
  43. std::cout << "Oh Oh... Unknown Exception :(" << std::endl;
  44. }
  45.  
  46. return 0;
  47. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
UserID: 1001