c++ - tokenizing a string of data into a vector of structs? -


I have the following string data, which is being obtained through a TCP winsock connection, and an advanced tokenization , In the vectors of structures, where each strait represents a record.

  std :: string buf = "44: William: adama: Commander: Content \ n33: Laura: Roslin: President: Data \ n" struct table_t {std :: string key; Std :: string first; Std :: string final; Std :: string rank; Std :: extras; };   

Each record in the string is delimited with a carriage return. My attempt is to split records, but so far the fields are not being divided:

  tokenize zero (std :: string & str, std :: vector & lt; String & gt; record) {// Skip start at delimiter Std :: string :: size_type lastPos = str.find_first_not_of ("\ n", 0); // Find the first "non-delimiter" Std :: string :: size_type pos = str.find_first_of ("\ n", last pauses); Whereas (std :: string :: npos! = Pos || std :: string :: npos! = LastPos) {// token found, add it to vector record push_back (str.substr (last pose, position - last pose) ); // Leave the "Delimiter" Note "no_of" last pause = str.find_first_not_of ("\ n", position); // Find the next "non-delimiter" status = str.find_first_of ("\ n", last pause); }}   

It seems that to replicate the code, each record will be repeated in the structure by the colon (internal area separator) and each stroke will be pushed into a vector. I'm sure there is a better way to do this or maybe the design is wrong in itself.

Thanks for any help.

To break the wire in the records, I use the istringstream, if only because it is simple When I want to read from a file then for the change token change, the most obvious solution is to promote: regex, therefore:

  std :: vector & lt; Table_t & gt; Parse (std :: istream and input) {std :: vector & lt; Table_t & gt; Retval; Std :: string line; While {std :: getline (input, line)) {static bust :: reggaex particle pattern ("\ [[^ ^] * \): \ ([^:] * \): \ ([^:] * \ ): \ ([^:] * \): \ ([^:] * \) "); Promotion: nasty matching; {Regex_match (line, match, pattern)} {// error handling ...} {retval.push_back (table_t (Milan [1], Milan [2], Milan [3], Milan [4], Milan [ 5])); }} Return returns; } (I have assumed the logical constructor for table_t. Also, there is a long tradition in C that the names ending in _t are type type, so you might have some other conference. )   

Comments