From: Grayham on
On Mon, 01 Feb 2010 10:13:32 +0000, Ben Cottrell wrote:

> Grayham wrote:
>> 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());
>
> There are several different things going on in that expression; would it
> help to mentally break that line down into two separate expressions?
> specifically, determining the outcome of find before reaching the return
> statement.
>
> std::string::const_iterator i =
> find(url_ch.begin(), url_ch.end(), c);
> return !(isalnum(c) || i != url_ch.end());
>
> I happen to have the book in front of me, I can see that there's a
> similar example using find_if on pg 103/104, which makes use of the (i
> != str.end()) construct and seems to explain in more detail about the
> use of an iterator as a return value.

That helped loads.

It's just comparing two iterators and returning a true or false.

What I needed to do was ignore the find(url_ch.begin(), url_ch.end(), c)
and just focus on it's returned value I.e iterator != iterator.

I was getting to bogged down with the function and ignoring it's purpose.
I could not see the wood for the trees


Thank you for your help and thank you to everyone else who chipped in
there. Its's appreciated.


Grayham
p.s it was obvious.
From: Richard Heathfield on
Grayham wrote:

<snip>

> p.s it was obvious.

It always is, once you know it.

There are only three kinds of problem: "impossible" (which means you
don't yet know how to solve it), "trivial" (which means you've already
solved it), and "covered in the literature" (which means that you
haven't actually solved it yourself, but you think you remember reading
that someone else has).

--
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: Herbert Rosenau on
On Sun, 31 Jan 2010 19:03:48 UTC, Grayham <no(a)spam_for_me.com> wrote:

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

It's simple: The operator yields one of two possible results:

0 == false == left hand operand is NOT unequal right hand operand
1 == true == left hand operand is unequal right hand operand.

There is no rule in C that forbids to store the result of an
comparsion in an variable, use it as value in an return statement, or
simply use it as an operand of another operator instead only to use it
immediately.

So it is common to build statements like

a = b < c;
x = y == z;

and use that value later on for another comparsion.

So your complicated return statement gets interpreted as

if c is either alpha or numeric then compare the result of that what
find() returns with that what url_ch.end() returns. The result of that
comparsion (see above!) is sent to the ! operator and that result is
returned to the caller of the function the return statement is in.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2R Deutsch ist da!