473,385 Members | 1,720 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,385 software developers and data experts.

split string contain special token?

Hi, all:
I want to implement a function (using std::string and std::vector)
split a string contain special token into vector.
eg:
string = "alex delia john"

==>

vector[0] = "alex";
vector[1] = "delia";
vector[2] = "john";

I want use some stl algorithms library function,but I don't kown which
algorithms function is suitable.
Thanks !
Alex 2005.01.12

Jul 22 '05 #1
15 6750
ze*******@gmail.com wrote:
Hi, all:
I want to implement a function (using std::string and std::vector)
split a string contain special token into vector.
eg:
string = "alex delia john"

==>

vector[0] = "alex";
vector[1] = "delia";
vector[2] = "john";

I want use some stl algorithms library function,but I don't kown which
algorithms function is suitable.
Thanks !

You might try the new Boost String Algorithm library:

http://www.boost.org/doc/html/string....html#id576776

Jonathan
Jul 22 '05 #2
I know boost string can split such string literal,but I want to
implement 'split' using stl.

Thanks your reply!

Jul 22 '05 #3

<ze*******@gmail.com> wrote in message
I know boost string can split such string literal,but I want to
implement 'split' using stl.


How about this -
string Str = "alex delia john";
std::istringstream iss( Str );
std::vector<string> vec( (std::istream_iterator<string>(iss)),
std::istream_iterator<string>() );

HTH,
Sharad
Jul 22 '05 #4
ze*******@gmail.com wrote:
I know boost string can split such string literal,but I want to
implement 'split' using stl.
For homework, I guess?
Thanks your reply!


You're welcome.

Jonathan
Jul 22 '05 #5
NO!
I just want to write a helper function for my workmates.

Jul 22 '05 #6

<ze*******@gmail.com> skrev i en meddelelse
news:11**********************@f14g2000cwb.googlegr oups.com...
NO!
I just want to write a helper function for my workmates.

The best thing would be to give your workmates a boost using boost. Here you
get well-documented, tested code. Much better than anything you can expect
yourself to deliver.

/Peter
Jul 22 '05 #7
> How about this -
string Str = "alex delia john";
std::istringstream iss( Str );
std::vector<string> vec( (std::istream_iterator<string>(iss)),
std::istream_iterator<string>() );

#include <string>
#include <vector>
#include <sstream>
int main(int, char**)
{
std::string Str = "alex de-lia jo-hn";
std::istringstream iss( Str ); // iss is a stream, that has the data
of Str
std::vector<std::string> my_tokens(
(std::istream_iterator<std::string>(iss)),
std::istream_iterator<std::string>()
);
for (int i=0; i<my_tokens.size(); ++i)
{
printf("%s\n", my_tokens[i].c_str());
}
}

I don't get it quite. Especially this part:

(std::istream_iterator<std::string>(iss)),
std::istream_iterator<std::string>());

the first is an iterator to the begin of the string - what is the
second one? How does the spitting occour? Can I use different
seperators? Like ",-." ?
That's a terrific source code!
-Gernot
Jul 22 '05 #8
ze*******@gmail.com wrote:
Hi, all:
I want to implement a function (using std::string and std::vector)
split a string contain special token into vector.
eg:
string = "alex delia john"

==>

vector[0] = "alex";
vector[1] = "delia";
vector[2] = "john";

I want use some stl algorithms library function,but I don't kown which algorithms function is suitable.


This comes up fairly frequently. You can either use stringstream
approach or something like the utility I posted here a few times. See
the link below:

http://groups-beta.google.com/group/...9335f1c396d288

I would have reposted the code, but I'm stuck using google for a while
to post and it messes up code indentation.

Brian

Jul 22 '05 #9
Peter Koch Larsen wrote:
<ze*******@gmail.com> skrev i en meddelelse
news:11**********************@f14g2000cwb.googlegr oups.com...
NO!
I just want to write a helper function for my workmates.
The best thing would be to give your workmates a boost using boost.

Here you get well-documented, tested code. Much better than anything you can expect yourself to deliver.

I see this a lot, but I'll point out that many real projects don't
allow the programmers to drag in whatever third-party libraries they
feel like. It would take a long period of evaluation (as the Boost
stuff is non-standard and not provided by a vendor you can go to to
complain).

Brian

Jul 22 '05 #10
> I don't get it quite. Especially this part:

(std::istream_iterator<std::string>(iss)),
std::istream_iterator<std::string>());


The first is an iterator to read strings from the input stream iss. The
second is what I'd call an EOF-iterator -- when the first iterator
reaches the end of the input stream, it will compare equal to the
second iterator.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 22 '05 #11
Default User wrote:
Peter Koch Larsen wrote:
The best thing would be to give your workmates a boost using boost.
Here you get well-documented, tested code. Much better than anything
you can expect yourself to deliver.

I see this a lot, but I'll point out that many real projects don't
allow the programmers to drag in whatever third-party libraries they
feel like. It would take a long period of evaluation (as the Boost
stuff is non-standard and not provided by a vendor you can go to to
complain).
Even if you can't use the Boost Libraries for some bureaucratic reason, you
still may be able to copy the implementation of a useful algorithm, or
reimplement the algorithm based on the boost design and informed by its
implementation.
Brian


Jonathan
Jul 22 '05 #12

"Jerry Coffin" <jc*****@taeus.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
I don't get it quite. Especially this part:

(std::istream_iterator<std::string>(iss)),
std::istream_iterator<std::string>());


The first is an iterator to read strings from the input stream iss. The
second is what I'd call an EOF-iterator --


From 14882:

istream_iterator();

1 Effects: Constructs the end*-of-*stream iterator.

Now you know what to call it. :-)
The details, for anyone interested:

24.5.1 Template class istream_iterator

1 istream_iterator reads (using operator>>) successive elements
from the input stream for which it was constructed. After it
is constructed, and every time ++ is used, the iterator reads
and stores a value of T. If the end of stream is reached
( operator void*() on the stream returns false), the iterator
becomes equal to the end*-of-*stream iterator value. The constructor
with no arguments istream_iterator() always constructs an end of
stream input iterator object, which is the only legitimate iterator
to be used for the end condition. The result of operator* on an end
of stream is not defined. For any other iterator value a const T&
is returned. The result of operator*-> on an end of stream is not
defined. For any other iterator value a const T* is returned. It
is impossible to store things into istream iterators. The main
peculiarity of the istream iterators is the fact that ++ operators
are not equality preserving, that is, i == j does not guarantee at
all that ++i == ++j. Every time ++ is used a new value is read.

2 The practical consequence of this fact is that istream iterators
can be used only for one*-pass algorithms, which actually makes
perfect sense, since for multi-pass algorithms it is always more
appropriate to use in*-memory data structures.

3 Two end*-of*-stream iterators are always equal. An end*-of*-stream
iterator is not equal to a non-*end*-of-*stream iterator. Two non*-
end*-of-*stream iterators are equal when they are constructed from
the same stream.
-Mike
Jul 22 '05 #13
On 12 Jan 2005 19:48:22 -0800 in comp.lang.c++, ze*******@gmail.com wrote,
I want to implement a function (using std::string and std::vector)
split a string contain special token into vector.
eg:
string = "alex delia john"


http://groups.google.com/gr*********....earthlink.net

Jul 22 '05 #14
> istream_iterator();

1 Effects: Constructs the end*-of-*stream iterator.

Now you know what to call it. :-)


I guess so. If nothing else, this is proof that (contrary to popular
belief) I'm _not_ the most verbose person on earth! :-)

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 22 '05 #15
Jonathan Turkanis wrote:
Default User wrote:
[Boost]
I see this a lot, but I'll point out that many real projects don't
allow the programmers to drag in whatever third-party libraries they feel like.

Even if you can't use the Boost Libraries for some bureaucratic reason, you still may be able to copy the implementation of a useful algorithm, or reimplement the algorithm based on the boost design and informed by its implementation.

That's true, depending on how much one Boost element relies on others
(I haven't looked into the implementations very deeply). I also don't
know what the licensing issues are, and of course this isn't really the
forum to discuss them.


Brian

Jul 22 '05 #16

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

Similar topics

5
by: Andy Mee | last post by:
Hello one and all, I'm developing an Asp.NET system to take a CSV file uploaded via the web, parse it, and insert the values into an SQL database. My sticking point comes when I try to split()...
4
by: indranil b | last post by:
void tokenise(const string& data, const string& separator, vector<string>& fields) { size_t n = data.length(); size_t start = data.find_first_not_of(separator); while (start < n) { size_t stop...
5
by: Vamsi | last post by:
Hi, I am trying a basic opearation of splitting a multiline value to an array of single lines(Actually making Address into AddressLine1, AddressLine2). I used Environment.NewLine in split, I...
4
by: dchan1 | last post by:
String line = " I am new to c# "; How to use Regex to split the string so that It would return a String array with: token = "I" token = "am" token = "new" token =...
22
by: Sven-Thorsten Fahrbach | last post by:
Hi Does anybody know of a library that offers a function to split pathnames. It should work somewhat like the following code snippet: ----------------- char *path =...
15
by: nagar | last post by:
I need to split a string whenever a separator string is present (lets sey #Key(val) where val is a variable) and rejoin it in the proper order after doing some processing. Is there a way to use...
10
by: teddyber | last post by:
Hello, first i'm a newbie to python (but i searched the Internet i swear). i'm looking for some way to split up a string into a list of pairs 'key=value'. This code should be able to handle this...
6
by: giffy | last post by:
HOW to get the spaces out of a delimited string ?? here is a usage scenario where split() seems not to work String origAVNListStr = "~`L~`L~`L~`~`~`L~`"; String strArr =...
5
mickey0
by: mickey0 | last post by:
hello, I have to read a text and I decided to split it with Scanner class. private Vector<String> splitWords(Scanner s) { Vector<String> v = new Vector<String>(); while ( s.hasNext() )...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...

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.