I have a couple of array’s:
const string a_strs[] = {'cr=1', 'ag=2', 'gnd=U', 'prl=12', 'av=123', 'sz=345', 'rc=6', 'pc=12345'}; const string b_strs[] = {'cr=2', 'sz=345', 'ag=10', 'gnd=M', 'prl=11', 'rc=6', 'cp=34', 'cv=54', 'av=654', 'ct=77', 'pc=12345'};
which i then need to parse out for ‘=’ and then put the values in the struct. (the rc key maps to the fc key in the struct), which is in the form of:
struct predict_cache_key { pck() : av_id(0), sz_id(0), cr_id(0), cp_id(0), cv_id(0), ct_id(0), fc(0), gnd(0), ag(0), pc(0), prl_id(0) { } int av_id; int sz_id; int cr_id; int cp_id; int cv_id; int ct_id; int fc; char gnd; int ag; int pc; long prl_id; };
The problem I am encountering is that the array’s are not in sequence or in the same sequence as the struct fields. So, I need to check each and then come up with a scheme to put the same into the struct.
Any help in using C or C++ to solve the above?
This shouldn’t be too hard. Your first problem is that you don’t have a fixed sized array, so you’d have to pass the size of the array, or what I’d prefer you make the arrays NULL-terminated, e.g.
const string a_strs[] = {'cr=1', 'ag=2', 'gnd=U', NULL};Then I would write a (private) helper function that parse the string:
then you can do what qrdl has suggested.
in a simple for loop:
EDIT: you should probably use long instead of int and atol instead of atoi, because your prl_id is of the type long. Second if there could be wrong formated numbers after the ‘=’, you should use strtol, which can catch errors.