I need...

This is a discussion on I need... within the C++ Programming forums, part of the General Programming Boards category; I need an explanation of part of the following function that is highlighted in red (possible typo). At first I ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    56

    Possible typo

    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.
    Code:
    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());
    }
    Last edited by swappo; 06-21-2009 at 10:41 PM. Reason: edited for clarity

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    find takes three arguments; where to start, where to stop, and what to look for. The result is to not equal (!=) ch.end.

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,175
    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.

    Soma

    Code:
    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;
    return b4;

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    56
    You need a better title.
    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.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by swappo View Post
    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.
    What do you mean, what is the close parenthesis for? It closes the open parenthesis:
    Code:
    find(url_ch.begin(), url_ch.end(), c)
        ^--start                   end--^

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    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().
    Makes sense?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    56
    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.

  9. #9
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    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.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by CodeMonkey View Post
    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.

    Code:
    return !(isalnum(c)
        || find(url_ch.begin(), url_ch.end(), c) != url_ch.end());
    Makes it pretty obvious that the second line is part of a larger expression.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Or even something like:

    Code:
    return 
    	!(
    		isalnum(c) 
    		||
    	        find(url_ch.begin(), url_ch.end(), c) != url_ch.end()
    	);
    One more thing. The canonical convention for boolean functions is the positive sense, eg:

    Code:
    bool url_char(char c);
    In other words, negation would done by the user code, not the function itself.



    ITSA
    Socket Library!

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21