arnuld wrote:
hey, it works, thanks :-) but i did not understand what "string::npos"
does.
npos is the null-position or no-position and it's a value defined by the
string implementation as "no position within this string". Each
implementation can create whatever number it wants to (negative,
MAX_INT, etc) so, to do a portable code you should never compare it to
the number you have now as "end-of-string" but to npos.
As zero is the first position it cannot be used to check if the find is
successful or not such as in:
if (!string.find("foo"))
or
if ((pos = string.find("foo")) == 0)
In my implementation, string::npos is 4294967295 (or max unsigned int)
as it does not allow strings bigger than that (I suppose). You can check
yours by simply:
cout << string::npos << endl;
But you should never use that value to check if the position is valid or
not.
Note that you should set pos to npos at the beginning because, if
something fails with find you will not be locked in that loop.
what exactly the "haystack" is ?
Literally is horse's food (normally loads of them), but it's a common
term used in search algorithms because of the phrase "finding a needle
in a haystack".
In algorithms phraseology. 'haystack' is the text or collection of
things you have and 'needle' is what you want to find there.
cheers,
--renato
--
Reclaim your digital rights, eliminate DRM, learn more at
http://www.defectivebydesign.org/what_is_drm