STL: Converting a string into a list of string

B

blrmaani

Here is what I want:

string s1 = "This is a list of string";
list<string> s2 = s1.some_method();

Now, I should be able to traverse list s2 and get each
member ( which is of type 'string' ).


I know that this can be achieved using strtok. But I was wondering
if there is any string member function which returns me a list of string?

thanks in advance
blrmaani
 
R

Russell Hanneken

blrmaani said:
Here is what I want:

string s1 = "This is a list of string";
list<string> s2 = s1.some_method();

Now, I should be able to traverse list s2 and get each
member ( which is of type 'string' ).

I know that this can be achieved using strtok. But I was wondering
if there is any string member function which returns me a list of string?

There's no string member function that returns a list of strings.

Here are two ways to do what you want (assuming you want to tokenize the
string on whitespace):

SOLUTION 1: Use istringstream

#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
#include <sstream>
#include <string>

int main()
{
std::string s("This is a list of string");
std::istringstream iss(s);

typedef std::istream_iterator<std::string> StringReader;
StringReader first(iss);
StringReader last;

// Initialize a list by reading from iss (implicitly using the
// >> operator)
typedef std::list<std::string> StringList;
StringList myList(first, last);

// Output the contents of the list
typedef std::eek:stream_iterator<std::string> StringWriter;
std::copy (myList.begin(), myList.end(),
StringWriter(std::cout, "\n"));
}

Here's the output:

This
is
a
list
of
string


SOLUTION 2: Use boost::tokenizer

This is a bit off-topic for this newsgroup, since it involves a
non-standard library. But you might be interested in using the Boost
library's tokenizer template (see http://www.boost.org/).

#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
#include <string>
#include <boost/tokenizer.hpp>

int main()
{
// Make a tokenizer object from a string (tokenized on
// whitespace)
std::string s("This is a list of string");
boost::tokenizer<> tokens(s);

// Create a list from the tokens
typedef std::list<std::string> StringList;
StringList myList(tokens.begin(), tokens.end());

// Output the contents of the list
typedef std::eek:stream_iterator<std::string> StringWriter;
std::copy (myList.begin(), myList.end(),
StringWriter(std::cout, "\n"));
}

The output:

This
is
a
list
of
string

Hope that helps.

Regards,

Russell Hanneken
(e-mail address removed)
Remove the 'g' from my address to send me mail.
 
D

Default User

blrmaani said:
Here is what I want:

string s1 = "This is a list of string";
list<string> s2 = s1.some_method();

Now, I should be able to traverse list s2 and get each
member ( which is of type 'string' ).

Why do you want a list vs. a vector?
I know that this can be achieved using strtok. But I was wondering
if there is any string member function which returns me a list of string?


There's nothing built-in. You can do it yourself by creating a strstream
from the original string and reading in each substring. Alternatively,
I'll give you the Explode() routine I wrote, which works basically like
the one in PHP. I'll leave it as an exercise for you to change it from
vector to list if you are set on that.


#include <vector>
#include <string>

// breaks apart a string into substrings separated by a character string
// does not use a strtok() style list of separator characters
// returns a vector of std::strings

std::vector<std::string> Explode (const std::string &inString,
const std::string &separator)
{
std::vector<std::string> returnVector;
std::string::size_type start = 0;
std::string::size_type end = 0;

while ((end = inString.find (separator, start)) != std::string::npos)
{
returnVector.push_back (inString.substr (start, end-start));
start = end + separator.size();
}

returnVector.push_back (inString.substr (start));

return returnVector;


Brian Rodenborn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top