So, I have the following line of data, which is accepted through the TCP winsock connection and wants to do extended tokenization, into the vector structs, where each structure represents one record.
std::string buf = "44:william:adama:commander:stuff\n33:luara:roslin:president:data\n" struct table_t { std::string key; std::string first; std::string last; std::string rank; std::additional; };
Each entry in the line is limited to carriage returns. My attempt to split records, but have not yet divided the fields:
void tokenize(std::string& str, std::vector< string >records) { // Skip delimiters at beginning. std::string::size_type lastPos = str.find_first_not_of("\n", 0); // Find first "non-delimiter". std::string::size_type pos = str.find_first_of("\n", lastPos); while (std::string::npos != pos || std::string::npos != lastPos) { // Found a token, add it to the vector. records.push_back(str.substr(lastPos, pos - lastPos)); // Skip delimiters. Note the "not_of" lastPos = str.find_first_not_of("\n", pos); // Find next "non-delimiter" pos = str.find_first_of("\n", lastPos); } }
It seems completely unnecessary to repeat all of this code again to further label each entry with a colon (internal field separator) in the structure and push each structure into a vector. I am sure there is a better way to do this, or the design itself is wrong.
Thanks for any help.
source share