Author | Message | Time |
---|---|---|
Sidoh | Does anyone know (or have) a function writen (or in their posetion) in C++ similar to the visual basic Split() function? | September 26, 2003, 8:12 PM |
Zakath | What does Split do? | September 26, 2003, 8:26 PM |
iago | No, it would be pretty difficult to write something like that in c++. I would recommend looking into the "foreach" command in perl, or this clever use of strtok: [code] for(char *p = strtok(Packet, " "); p; p = strtok(NULL, " ")) { // ... } [/code] In that, it will run that loop once for each token in the string, much like perl's foreach. Warning: This will modify the original string. | September 26, 2003, 8:33 PM |
K | I wrote this just for you. ;D Shouldn't be hard to modify it to take multiple delimeters -- look at basic_string::find_first_of() if you want to do that. [code] #include <string> #include <vector> std::vector<std::string> SplitStr(const std::string& text, const std::string& delimeter) { size_t pos = 0; size_t oldpos = 0; size_t delimlen = delimeter.length(); std::vector<std::string> result; while(pos != std::string::npos) { pos = text.find(delimeter, oldpos); result.push_back(text.substr(oldpos, pos - oldpos)); oldpos = pos + delimlen; } return result; }[/code] Edit: come on iago, character arrays and strok in c++? ::) | September 26, 2003, 8:39 PM |
iago | Mine is muchm much more efficient, which tends to be a major factor in my code! :-P | September 26, 2003, 8:41 PM |
K | Or if you only want to split on whitespace (end of line, space, tab, etc) you could use a stringstream: [code] #include <string> #include <sstream> #include <vector> vector<string> SplitStr(const string& text) { stringstream stream; vector<string> result; string tmp; stream << text; while(!stream.eof()) { stream >> tmp; result.push_back(tmp); } return result; } [/code] | September 26, 2003, 8:52 PM |
iago | That's even more eew! | September 26, 2003, 10:05 PM |
Sidoh | lol iago stupid c++, can't make a split() function, lazy bastards... i actually like C++ better then vb, "funner" to code.. hey iago, do you use strtok to split bnet returns? or do you have your own writen function? | September 26, 2003, 11:26 PM |
iago | To split bnet returns? What are you talking about? I use a buffer class I wrote to process b.net packets, if that's what you mean. It can be found at http://iago.valhallalegends.com . | September 26, 2003, 11:45 PM |
Maddox | [quote author=Sidoh link=board=17;threadid=2843;start=0#msg22295 date=1064618810] lol iago stupid c++, can't make a split() function, lazy bastards... i actually like C++ better then vb, "funner" to code.. hey iago, do you use strtok to split bnet returns? or do you have your own writen function? [/quote] I think you are refering to extracting strings from packet data, but why would you use strtok() to do that? The strings are null terminated. | September 27, 2003, 3:04 AM |
Arta | You're still thinking in VB. Stop it. Think in C++. Don't go "This is how I'd do X in VB, what's the equivalent in C++?". Do something more like: "What facility does C++ provide that will allow me to do X?". Your code will end up being much more efficient if you change your mindset a bit. | September 27, 2003, 3:07 AM |