fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <vector>
  4. #include <string>
  5. #include <cstring>
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10. struct kvpair {
  11. /* Pointers to the key and value data */
  12. void *key;
  13. void *value;
  14.  
  15. /* Size of the key and value data in bytes */
  16. uint32_t keysz;
  17. uint32_t valuesz;
  18. };
  19.  
  20. vector<struct kvpair> parseWordcInput (string line, string delimiters) {
  21. struct kvpair kv;
  22. size_t pos_start = 0, pos_end, delim_len = delimiters.length();
  23. vector<struct kvpair> res;
  24.  
  25. while ((pos_end = line.find (delimiters, pos_start)) != string::npos) {
  26. string token;
  27. token = line.substr (pos_start, pos_end - pos_start);
  28. pos_start = pos_end + delim_len;
  29. char *cstr1 = new char[token.length() + 1];
  30. strcpy(cstr1, token.c_str());
  31. cstr1[token.length()] = '\0';
  32. //kv.key = static_cast<void*>(&token);
  33. kv.key = (void *)cstr1;
  34. kv.keysz = token.length() + 1;
  35. kv.value = NULL;
  36. kv.valuesz = 0;
  37.  
  38. res.push_back (kv);
  39. }
  40.  
  41. string token = line.substr (pos_start);
  42. kv.key = static_cast<void*>(&token);
  43. kv.keysz = token.length()+1;
  44. kv.value = NULL;
  45. kv.valuesz = 0;
  46. res.push_back (kv);
  47.  
  48. return res;
  49. }
  50.  
  51. int main() {
  52. string str = "adsf-+qwret-+nvfkbdsj-+orthdfjgh-+dfjrleih";
  53. string delimiters = "-+";
  54. vector<struct kvpair> kvpairs = parseWordcInput (str, delimiters);
  55.  
  56. for (auto i : kvpairs) cout << i.key <<" "<< i.keysz << endl;
  57.  
  58. return 0;
  59. }
Success #stdin #stdout 0s 4276KB
stdin
Standard input is empty
stdout
0x55cdc155bef0 5
0x55cdc155bf30 6
0x55cdc155bf10 9
0x55cdc155c000 10
0x7ffe9d6b5d30 9