Now I know there are those of you out there who will look at this code and see fifty better ways of doing it, or some will look at it and say "There's already a way of doing that," or will look at it and say "That's not very optimized." Or any other plethora of things. But, I don't care. It wasn't written to be optimized, I didn't write it the best way possible, or anything like that, simply because I'm not the best. I'm a mediocre programmer who was just doing this as a test for myself to see if I could successfully write a split function for strings. So without further ado, here it is.
EDIT: I just noticed there was some old code that doesn't need to be there. It's better now.
#include #include #include using namespace std;vector split(char sep, const string &str);void main() { string rooms = "Room 1;Room 1 Description;Exits;Parameter 4;Fuck off!"; vector roominfo; roominfo = split(';', rooms); for (int i = 0; i < (int)roominfo.size(); i++) cout << roominfo << endl;}/////////////////////////////////////////////////vector split(char sep, const string &str){ // Define "no more seperators found" static const basic_string <char>::size_type npos = -1; string strCopy = str; // Mak a copy so we can screw with it and not harm the original vector tmpString; size_t position; // Get the first position of the seperator position = strCopy.find_first_of(sep); // If position is ever <= zero, that means that there is no seperator in the string while (position != npos) { // Add the first slice to the array tmpString.push_back(strCopy.substr(0, position)); // We are erasing up to the position of the separator, plus one, to erase the // seperator as well. strCopy.erase(0, position + 1); // Get the next position of the seperator position = strCopy.find_first_of(sep); } // When the position is zero, that means that we have no more seperators. All that's // left is the last one, and goes into the last part of the array. tmpString.push_back(strCopy); return tmpString;}
I know, not the most glamorous, but it works wonderfully.
The outcome on her progress getting it done is, she got her own code working without having to look at mine. Her project is well off on it's way and I'm quite proud of her for what she's been able to accomplish thus far. She's learning quite well. :-)
:P