473,395 Members | 1,649 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

STL: Converting a string into a list of string

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
Jul 22 '05 #1
4 9080
blrmaani wrote:
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::ostream_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::ostream_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
rg********@pobox.com
Remove the 'g' from my address to send me mail.
Jul 22 '05 #2
On 27 Apr 2004 12:02:51 -0700 in comp.lang.c++, bl******@yahoo.com
(blrmaani) wrote,
string s1 = "This is a list of string";
list<string> s2 = s1.some_method();


I would do that using a quickie tokenizer template function such as
http://groups.google.com/gr*********....earthlink.net

Jul 22 '05 #3
blrmaani wrote:
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


http://linuxselfhelp.com/HOWTO/C++Pr...g-HOWTO-7.html.

HTH
--
Karthik

Humans please 'removeme_' for my real email.
Jul 22 '05 #4
blrmaani wrote:

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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Michael | last post by:
Guys, I'm trying to compile this code to build a huffman tree, but I'm getting a runtime error and my degugger ends up somewhere in the STL files. What am i doing wrong. I appreciate its not...
10
by: Der Andere | last post by:
I need to implement a sorted (ordered) list. STL has no sorted list type as far as I know. Is there a (straight) way to implement a sorted list using STL? BTW: The type of items in the list will...
6
by: Alex Gerdemann | last post by:
Hello, I am writing a program where I have a vector (std::vector<std:string> list) that I need to search many times. To accomplish this efficiently, I plan to sort the list using...
7
by: Tony Johansson | last post by:
Hello!! Assume I have a handle body pattern with classes called Handle and Body. In the Body class I store one int value for example 7 or some other integer value. In the Handle class I have...
0
by: Tony Johansson | last post by:
Hello! I have two classes called Handle which is a template class and a class Integer which is not a template class. The Integer class is just a wrapper class for a primitive int with some...
7
by: Sammy | last post by:
Its slow but Im in the process of converting some of my admittedly ugly code into STL. At the moment Im working to change a mess that I like to call a linked list when Im in a generous mood....
2
by: Bruce | last post by:
I am converting a DLL project from VC7 to VC8. The project is a managed C++ DLL. The project uses a non managed C++ library compiled an linked in VC7 which uses the STL. I just about got...
3
by: misu101 | last post by:
Hi, I have an application that defines a hash_set as: typedef hash_set<const char*, hash<const char*>, chunker_eqstr> StringSet; This code compiles OK with VS6 and SGI STL library. It is...
7
by: ademirzanetti | last post by:
Hi there !!! I would like to listen your opinions about inherit from a STL class like list. For example, do you think it is a good approach if I inherit from list to create something like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.