Explode() for C++? (aka split())
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
$array=explode("|","Value|Morestuff|WEEE!|OMGoO|Result");
?>
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.
Don''t listen to me. I''ve had too much coffee.
I got bored
:
Hopefully that gets through the mangling okay...


#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...

This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement