Using C string handling functions like strchr or memchr to parse char array into C++ key value map -
i have char array this:
"ca: abcd\0cb: abfg\0cc: afbv\0cd: 4567"
now ": "
splits key value while \0
separates pairs. want add key-value pairs map in c++ using strchr or memchr functions. suggestions?
something along these lines, perhaps:
const char source[] = "ca: abcd\0cb: abfg\0cc: afbv\0cd: 4567"; int len = sizeof(source); std::map<std::string, std::string> pairs; const char* end = source + len; const char* start = source; while (start < end) { const char* sep = strchr(start, ':'); const char* pair_end = strchr(sep + 2, '\0'); pairs.emplace(std::string(start, sep), std::string(sep + 2, pair_end)); start = pair_end + 1; }
Comments
Post a Comment