Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

FireSlash

Explode() for C++? (aka split())

This topic is 5648 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Yes, I am retarded. Whats the C++ version of the explode (PHP) or split (vb) functions? Kinda hard to parse text files without it :B

Share this post


Link to post
Share on other sites
Advertisement
Well, not that hard; you can just search through for the character you want. But if you don''t wanna do that yourself, check out the C function strtok() and the Boost::Tokenizer library.


Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites
I got bored :

  
#include <algorithm>


template <typename ContType, typename CharType>
void explode(ContType &Output, const CharType *Begin, const CharType *End, const CharType *SBegin, const CharType *SEnd) {
while(Begin < End) {
const CharType *Next = std::search(Begin,End,SBegin,SEnd);

Output.push_back(std::string(Begin,(Next-Begin)));

Begin = Next+1;
}
}

template <typename ContType, typename CharType>
inline void explode(ContType &Output, const CharType *Begin, const CharType *End, CharType Symbol) {
explode(Output,Begin,End,&Symbol,&Symbol + 1);
}

// DEMO:

#include <iostream>

#include <iterator>

#include <algorithm>

#include <list>

#include <string>


int main(void) {
const char Data[] = "Value|Morestuff|WEEE!|OMGoO|Result";
std::list <std::string> Exploded;

explode(Exploded, &Data[0], &Data[sizeof(Data)], ''|'');

std::copy(Exploded.begin(), Exploded.end(), std::ostream_iterator<std::string>(std::cout,"\n"));

return 0;
}


Hopefully that gets through the mangling okay...

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!