[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: String: can someone explain this...
From: |
Harald van Pee |
Subject: |
Re: String: can someone explain this... |
Date: |
Wed, 18 Oct 2000 21:26:38 +0200 |
Albert Schueller wrote:
>
> Hi,
>
> Why do the two couts in this snippet of code printout different things when
> response = "no"; but the same thing when response = "yes";?
>
The result of find_first_of() is of type size_type (size_t).
in general int is no size_type, because you need no negative value
for size types
-> if find_first_of() finds a position, 1 or 2 fits into int.
if not, the result is result.npos (in general a very big number, illegal
character position)
maybe 2^32-1 -> doesn't fit into int
-> use
size_t position;
and there will be no problem!
Regards
Harald
> ///////////////////////////////////////////////////////////////////////////
>
> #include <iostream>
> #include <string>
>
> int main() {
> string response("no");
> int position;
>
> cout << response.find_first_of("yY") << endl;
> position = response.find_first_of("yY");
> cout << position << endl;
>
> return 0;
> }
>
> --
> Albert Schueller Department of Mathematics
> Office Phone: 509-527-5140 Whitman College
> Public Key: http://carrot.whitman.edu/gpg.key Walla Walla, WA USA 99362