Prev: Can Someone please help me with my Computer stuff? 50194
Next: Rs 20,000 INR at stake in CodeWarrior
From: Grayham on 31 Jan 2010 14:03 Hello All I have been working my way through Accelerated c++ and have so far found it enjoyable interesting. I have currently just finished reading chapter 6 and there is one part I just can't get my head around. It's on page 107, last example. This Lines: return !(isalnum(c)|| find(url_ch.begin(), url_ch.end(), c) != url_ch.end()); My problem with this code is the "!= url_ch.end()" at the end of the statement. This to me suggest a conditional situation normally applied to a "while", "if" or "for" statement. I just can't work out how it's applied in this situation. I have tested the code and it does not work without it but I can't see why. What bugging my is this is probably obvious. Can someone help please. Thank You Grayham
From: Alf P. Steinbach on 31 Jan 2010 14:26 * Grayham: > Hello All > > I have been working my way through Accelerated c++ and have so far found > it enjoyable interesting. I have currently just finished reading chapter > 6 and there is one part I just can't get my head around. It's on page > 107, last example. > > This Lines: > return !(isalnum(c)|| > find(url_ch.begin(), url_ch.end(), c) != url_ch.end()); > > My problem with this code is the "!= url_ch.end()" at the end of the > statement. This to me suggest a conditional situation normally applied to > a "while", "if" or "for" statement. Yeah, most conditionals are simple comparisions, and the above is a comparision. The comparision yields 'false' or 'true', which are values of type 'bool'. You might try this: #include <iostream> int main() { using namespace std; cout << (2+2 == 4) << endl; cout << boolalpha; // Changes output representation of booleans. cout << (2+2 == 4) << endl; } Cheers & hth., - Alf
From: Richard Heathfield on 31 Jan 2010 14:36 Grayham wrote: <snip> > return !(isalnum(c)|| > find(url_ch.begin(), url_ch.end(), c) != url_ch.end()); > > My problem with this code is the "!= url_ch.end()" at the end of the > statement. This to me suggest a conditional situation normally applied to > a "while", "if" or "for" statement. The != operator takes two operands (call them X and Y, so the call is X != Y, okay?), and applies the following logic: if the two operands are equal, != yields the result 0. Otherwise, it yields the result 1. Yes, it's normally used in if(), while(), etc, but that's not written in stone. Try running this program: #include <stdio.h> int main(void) { int i; int j; printf(" 0 1 2 3 4\n"); printf(" -----------\n"); for(i = 0; i < 5; i++) { printf("%d", i); for(j = 0; j < 5; j++) { printf(" %d", i != j); } putchar('\n'); } return 0; } With any luck, a light will go "bing" in your head (or possibly a sound will flash!), and you'll understand. Otherwise, check back. -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ "Usenet is a strange place" - dmr 29 July 1999 Sig line vacant - apply within
From: Grayham on 31 Jan 2010 14:48 On Sun, 31 Jan 2010 20:26:22 +0100, Alf P. Steinbach wrote: > * Grayham: >> Hello All >> >> I have been working my way through Accelerated c++ and have so far >> found it enjoyable interesting. I have currently just finished reading >> chapter 6 and there is one part I just can't get my head around. It's >> on page 107, last example. >> >> This Lines: >> return !(isalnum(c)|| >> find(url_ch.begin(), url_ch.end(), c) != url_ch.end()); >> >> My problem with this code is the "!= url_ch.end()" at the end of the >> statement. This to me suggest a conditional situation normally applied >> to a "while", "if" or "for" statement. > > Yeah, most conditionals are simple comparisions, and the above is a > comparision. > > The comparision yields 'false' or 'true', which are values of type > 'bool'. > > You might try this: > > #include <iostream> > > int main() > { > using namespace std; > cout << (2+2 == 4) << endl; > cout << boolalpha; // Changes output representation of > booleans. cout << (2+2 == 4) << endl; > } > > > Cheers & hth., > > - Alf Hi Alf I ran your code and understand the results but I still don't understand with the code I wrote is why this "!= url_ch.end())" is needed at the end of the statement. I'm not moving through the string so why is it checking for the end. Why does the code not work like so? return !(isalnum(c)|| find(url_ch.begin(), url_ch.end(), c)); Can "return" be conditional as in "if" Thank you for the reply Grayham
From: Ike Naar on 31 Jan 2010 15:16 In article <01aa1559$0$22036$c3e8da3(a)news.astraweb.com>, Grayham <no(a)spam_for_me.com> wrote: >>> return !(isalnum(c)|| >>> find(url_ch.begin(), url_ch.end(), c) != url_ch.end()); >>> >>> My problem with this code is the "!= url_ch.end()" at the end of the >>> statement. This to me suggest a conditional situation normally applied >>> to a "while", "if" or "for" statement. > > [snip] > >I ran your code and understand the results but I still don't understand >with the code I wrote is why this "!= url_ch.end())" is needed at the end >of the statement. > > I'm not moving through the string so why is it checking for the end. Why >does the code not work like so? > >return !(isalnum(c)|| find(url_ch.begin(), url_ch.end(), c)); > >Can "return" be conditional as in "if" ``find(first,last,value)'' finds the first occurrence of ``value'' in the range [first ... last) and returns an iterator to the found occurrence, or ``last'' if ``value'' did not occur in the given range. So, by comparing the return value of find(first,last,value) with last, you can find out whether the range contained ``value'' or not.
|
Next
|
Last
Pages: 1 2 3 Prev: Can Someone please help me with my Computer stuff? 50194 Next: Rs 20,000 INR at stake in CodeWarrior |