how to split a string in c++?
Suppose I have two vectors of strings and a large string.
The large string contains a comma separated list of names followed by :: followed by another list of comma separated names.
If I wanted to store the first list of names in the first vector and the second list of names in the other vector, how should I go about doing it?
I thought about using an IStringStream and repeatedly calling GetLine, but I figured there was probably a better way to do it that I didn't know about.
Seeing as you mentioned vector's I can only assume that you're using std::strings. That being the case, that's actually very easy.
I'm not going to give you the exact code because that wouldn't be doing you any favors at all (you need to learn after all... ;) ) but I will point you in the right direction.
First, you should take a good look at this site. It's a great reference about string and its various functions.
There are several ways to do this. You can use the substr() function to break your string into 'tokens'. In order to know where to break it apart, you can use the find() function to build a temporary list of the positions where the commas are in your primary string and then iterate through that list generating substrings.
Hope this helps. Good luck!
I'm not going to give you the exact code because that wouldn't be doing you any favors at all (you need to learn after all... ;) ) but I will point you in the right direction.
First, you should take a good look at this site. It's a great reference about string and its various functions.
There are several ways to do this. You can use the substr() function to break your string into 'tokens'. In order to know where to break it apart, you can use the find() function to build a temporary list of the positions where the commas are in your primary string and then iterate through that list generating substrings.
Hope this helps. Good luck!
There's boost tokenizor and boost string algorithm.
Example:
Edit: Added links.
Example:
#include <iostream>#include <string>#include <vector>#include <boost/tokenizer.hpp>#include <boost/algorithm/string.hpp>using namespace std;using namespace boost;int main(){ // here's a string in the format you described, plus // two empty vectors v1 and v2 string text = "A,B,C::D,E,F"; vector<string> v1; vector<string> v2; // tokenize the string by the "::" character // and get an iterator to the first tokenizer result char_separator<char> sep("::"); tokenizer<char_separator<char>> tokens(text, sep); tokenizer<char_separator<char>>::iterator it = tokens.begin(); // split (defined in algorithm/string.hpp will tokenize // the string pointed to by it and store each token in // the specified container split(v1, string(*it), is_any_of(","), token_compress_on); ++it; split(v2, string(*it), is_any_of(","), token_compress_on);}
Edit: Added links.
I would just use the stringstream approach, to be honest. If you were reading whitespace-delimited words, you could do some fancy stuff with the standard library algorithm 'std::copy' and iterator adapter 'std::istream_iterator', but as it is, it'd be much more work to get something similar set up, versus writing the loop yourself.
Quote:Original post by Storyyeller
Wouldn't that be even more complicated then the StringStream method?
Yeah, you're right. String streams are better for that sort of thing... I tend to not use stringstreams but that's mostly because I'm not nearly as familiar with them as I should be... -_-
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement