split string contain special token?

Z

zealotcat

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
 
J

Jonathan Turkanis

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_algo/usage.html#id576776

Jonathan
 
Z

zealotcat

I know boost string can split such string literal,but I want to
implement 'split' using stl.

Thanks your reply!
 
S

Sharad Kala

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
 
P

Peter Koch Larsen

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
 
G

Gernot Frisch

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

Default User

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/comp.lang.c++/msg/e19335f1c396d288

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



Brian
 
D

Default User

Peter said:
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
 
J

Jerry Coffin

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

Jonathan Turkanis

Default said:
Peter Koch Larsen wrote:
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.

Jonathan
 
M

Mike Wahler

Jerry Coffin said:
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
 
J

Jerry Coffin

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! :)
 
D

Default User

Jonathan said:
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
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top