I need an explanation of part of the following function that is highlighted in red (possible typo). At first I thought the parenthesis to the right of c was a typo(this is from the book Accelerated C++) but the full code compiles and runs successfully. I don't understand why that parenthesis is there.
bool not_url_char(char c)
// characters, in addition to alphanumerics, that can appear in a \s-1URL\s0
static const string url_ch = "~;/?:@=&$-_.+!*'(),";
// see whether `c' can appear in a \s-1URL\s0 and return the negative
return !(isalnum(c) ||
find(url_ch.begin(), url_ch.end(), c) != url_ch.end());
find takes three arguments; where to start, where to stop, and what to look for. The result is to not equal (!=) ch.end.
You need a better title.
Anyway... when you come across something like this break it into multiple lines.
Edit: Actually, if you don't understand it after breaking things up you should start over.
iter i1 = url_ch.begin();
iter i2 = url_ch.end();
iter i3 = find(v1, v2, c);
bool b1 = isalnum(c);
bool b2 = i3 != i2; // bool b2 = (i3 != i2);
bool b3 = b1 || b2;
bool b4 = !b3;
Sorry, I couldn't think of anything better. Actually, its the "c)" in the red that I don't understand. What is the end parenthesis for? It looks like a typo to me, but the code compiles and runs. The code in the book and in the provided code example each had that end parenthesis. If this is a concept in C++, I don't understand it. If it is a typo, I don't understand why it compiles and runs. Sorry if my original post wasn't clear.
You need a better title.
What do you mean, what is the close parenthesis for? It closes the open parenthesis:
Originally Posted by swappo
find(url_ch.begin(), url_ch.end(), c)
std::find is defined as follows:
std::find(start_it, end_it, char_to_find)
If std::find cannot find char_to_find in the range start_it to end_it, it simply returns end_it.
And the parameter you pass to end_it is url_ch.end().
You also need an editor that can highlight matching parentheses. Such as, well, nearly every programmer's editor out there, with the right settings. (I like jEdit.)
I think I understand it now. I think it was the return statement being on two lines that threw me off. It looked as if the != url_ch.end()) was part of find(). Sometimes I have trouble making sense of other peoples code.
Yes, the two-line return at first confused me :) It was the last closing parentheses that made me force myself to look at it twice.
Which is exactly why, when I split a line of code, I put the related operator on the beginning of the next line instead of the end of the previous.
Originally Posted by CodeMonkey
Makes it pretty obvious that the second line is part of a larger expression.
|| find(url_ch.begin(), url_ch.end(), c) != url_ch.end());
Or even something like:
One more thing. The canonical convention for boolean functions is the positive sense, eg:
find(url_ch.begin(), url_ch.end(), c) != url_ch.end()
In other words, negation would done by the user code, not the function itself.
bool url_char(char c);