For a small library I need a function like C's strtok. I doubt it's correct
to use it as it's ANSI but not C++. Is there any equivalent function in the
Standard Library that serves the same purpose? If not, does anyone have a
smart idea or a snippet handy?
The library will be compiled under Windows NT/2000, Linux and Solaris ....
so I cannot use any compiler specific functions or platform dependant stuff.
Thx!
Marc
[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
> Hi all!
> For a small library I need a function like C's strtok. I doubt it's correct
> to use it as it's ANSI but not C++. Is there any equivalent function in the
> Standard Library that serves the same purpose? If not, does anyone have a
> smart idea or a snippet handy?
Question: what do you mean by ANSI but not C++? ANSI is a standards
organization. Do you mean ANSI C? If something is ANSI C it's also ANSI C++
(Almost always). An ANSI C program wll therefore be an ANSI C++ program,
even if it contains strtok(). You might want to use <cstring> to get
std::strtok() to show you're really programming C++.
> Thx!
> Marc
HTH,
--
Michiel Salters
Michiel...@cmg.nl
sal...@lucent.com
Check libstdc++ FAQ. It used to reside somewhere on gnu.org,
but now the only working copy I found is at
http://docs.et.fh-osnabrueck.de/doku/libstdcpp/21_strings/howto.html
--
Alexander Belopolsky
Brainbench C/C++ Programming “Most Valuable Professional”
www.brainbench.com
Sent via Deja.com
http://www.deja.com/
A friend of mine once asked for something that worked like Java's
StringTokenizer class, and I hacked this up:
#include <iostream>
#include <string>
// using namespace std; // I know, I know, old g++
class tokenizer {
string s, delim;
string::size_type pos;
public:
tokenizer( string xs, string xdelim ) : s(xs), delim(xdelim) {
pos = s.find_first_not_of(delim);
}
bool has_more_tokens() { return pos!=string::npos; }
string next_token() {
string::size_type end_pos = s.find_first_of(delim,pos);
string token = s.substr( pos, end_pos-pos );
pos = s.find_first_not_of(delim,end_pos);
return token;
}
};
int main() {
string test = " Tokenize me, \n baby! ";
string delim = "\n\t ";
tokenizer t( test, delim );
while( t.has_more_tokens() )
cout << t.next_token() << endl;
return 0;
}
It may have a bug or two, but it should be enough to work with to create
something that suits your needs, I'd guess.
--
Brian M. McNamara lor...@acm.org : I am a parsing fool!
** Reduce - Reuse - Recycle ** : (Where's my medication? ;) )
std::strtok is defined in <cstream> even though I note that Stroustrup,
among other popular references, does not mention this fact.
However strtok has implementation problems, including that it is not
reentrant because it stores progress information in a static table. It also
works by modifying the input string, which is undesirable in many
applications. If your compiler supports it strtok_r is safer (defined by
Common UNIX and therefore certainly present in Linux and Solaris, but
probably not in Win NT). However strtok_r is not in the C++ standard. The
other problem, of course, is that strtok does not use std::string.
With good reason: there's no such header. <g> I assume that's a typo for
<cstring>.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Contributing Editor, C/C++ Users Journal (http://www.cuj.com)
(Disclaimer: Those are from memory. Check your string library to verify
the names.)
--
sma at sohnen-moe dot com