can you help me use the stl string class?
Hi. i have been reading my book on the stl string section but i am having much difficulty. this is what i need to do in c++,
a string that will be passed to my function will look like this:
"item 123/field 1 = something/ field 2 = something/ field 3 = something/item 234 / field 1 = something/ field 2 = something/ fields 3 = somethind/ item 125 / field 1 = something/ field 2 = something/ fields 3 = somethind/ ENDLINE/"
how can i use the stl string funtions to extract the item number, and then extract each field for the item number so i can store them in a structure where i can update them whenever i need to?
since i know field 1 is between the first instance of '/' and the second '/' and field 2 is between the second '/' and the third '/', how can i extract whats in between the '/' and the next '/'?
The stl string class is cool, but it isn't magic. Look for the RegEx lib around the net. Or, use XML and use an XML parser to get that information.
The only way I know of to do it with a string is to write a parser for this information yourself.
The only way I know of to do it with a string is to write a parser for this information yourself.
Write a little function that will split your string at the '/' characters. For instance, given "Field1 = 0/Field2 = 1"
It will return a std::vector<std::string> containing "Field1 = 0" and "Field2 = 1".
After that, loop through the vector, and for each string, extract what you need, and use atoi, atof for convert the value to what you need.
usefull strings methods for that will be : std::string::find and std::string::substr.
(I don't have time to write an example, sorry)
It will return a std::vector<std::string> containing "Field1 = 0" and "Field2 = 1".
After that, loop through the vector, and for each string, extract what you need, and use atoi, atof for convert the value to what you need.
usefull strings methods for that will be : std::string::find and std::string::substr.
(I don't have time to write an example, sorry)
Quote:Original post by paic
Write a little function that will split your string at the '/' characters. For instance, given "Field1 = 0/Field2 = 1"
It will return a std::vector<std::string> containing "Field1 = 0" and "Field2 = 1".
After that, loop through the vector, and for each string, extract what you need, and use atoi, atof for convert the value to what you need.
usefull strings methods for that will be : std::string::find and std::string::substr.
(I don't have time to write an example, sorry)
Like this I guess:
void TokenizeString(std::string const & pString, std::string const & pDelimiter, std::vector <std::string> & pResult){ if(pString.empty() || pDelimiter.empty()) return void(); else if(!pResult.empty()) pResult.clear(); size_t stIndexA = size_t(), stIndexB = pString.find(pDelimiter); if(stIndexB == std::string::npos) return void(); while(stIndexB != std::string::npos) { pResult.push_back(pString.substr(stIndexA, stIndexB - stIndexA)); stIndexB = pString.find(pDelimiter, stIndexA = stIndexB + pDelimiter.size()); } pResult.push_back(pString.substr(stIndexA, pString.size() - stIndexA));}
Edit: Forgot to say that this will only split the string where it finds the delimiter (if any), then you'll need to extract your data from the tokens in the vector.
Quote:Original post by bakerIf you'd like to avoid reinventing the wheel, boost::tokenizer<> and boost::lexical_cast<> will do simple parsing and conversion for you. If you're doing this to learn though, writing a simple tokenizer yourself (as suggested above) shouldn't be too hard.
how can i use the stl string funtions to extract the item number, and then extract each field for the item number so i can store them in a structure where i can update them whenever i need to?
but as the title suggests, can you help with STL string class
not boost::
you can try something like this: here I wrote something for you :D
perhaps instead of using arrays, you may want to use the vector class because it's STL.
and maybe you want to modify that a little bit, to take in data_array[256][2]
and perhaps double parse, because you need to parse it by the '=' character as well.
not boost::
you can try something like this: here I wrote something for you :D
function parse_data(string data, //this is your string to be passed string separator, //you want to separate it by '/' string data_array[256] //where you want stored ){ int pos; for( i=0; i<=255;i++ ){ pos=data.find(separator); //find the position where there is a / if ( pos == 0 ) break; //if data has run out of strings break; data_array=data.substr(0,pos); //save into array data=data.substr(pos+1,data.length()); //change data excluding what was saved }}
perhaps instead of using arrays, you may want to use the vector class because it's STL.
and maybe you want to modify that a little bit, to take in data_array[256][2]
and perhaps double parse, because you need to parse it by the '=' character as well.
Quote:Original post by TradoneIMO suggesting the use of boost, where applicable, is a reasonable answer to any general C++ query. If someone asks for help implementing an array class, should you not recommend std::vector<>? If someone is struggling with character pointers, should you not suggest the use of std::string? I agree that rolling your own can be a good exercise, but otherwise I guess I don't get your point.
but as the title suggests, can you help with STL string class not boost::
Quote:
but as the title suggests, can you help with STL string class
not boost::
It's already been established that std::string doesn't have this functionality built in, so we need to use other code.
boost is a good suggestion for this case.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement