Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"

Anand Hariharan

+ Follow
since Aug 22, 2006
Anand likes ...
C++ Debian VI Editor
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Anand Hariharan

Your code has ALL the following characteristics:

  • Templates!
  • Nested classes
  • The inner class using the template parameter of the outer class
  • Implementing the member functions of the template classes outside the class definition

  • I am no language lawyer, but I think it is reasonable to assume that ANY of the above could make the head of a C++ language lawyer hurt!  

    I hope this helps:

    - Anand
    1 year ago
    Please provide the following details:

  • Definition of myGraph, including declaration of myGraphNode
  • Instantiation of the template class
  • The line that calls to output your intantiated object
  • 1 year ago

    Adam Szewczyk wrote:Greetings,
    I am trying to translate following java code into c++ code.
    I do not understand what Configuration.DEBUG is doing. What is the c++ equivalent for it? Will omitting it affect " if then " performance significantly?

    While your specific question regarding 'what Configuration.DEBUG is doing' was addressed and answered, just a note that unlike Java that requires an IEEE 754 based floating point representation, C++ does not require so. Also, C++ does not have functions such as double-to-long functions and one typically would have to use a cast. The behavior of the cast is also left undefined by the language (since the size of a double and the size of a long could be different).

    For most PC based compilers, such matters are not an issue though and your 'translation' from Java to C++ would appear to work just fine.
    4 years ago
    I see that you have figured out the problem. Just wanted to point out something:

    mark patindol wrote:

    You are relying on a compiler extension (i.e., it is not likely to work if you use another compiler or even the same compiler with different settings).

    The ISO standard for the C++ language (which is clearly what you are using, as opposed to the C language) does not allow you to declare an array whose size is specified only at run-time. If you were converting the code from Java, the Java code would have done a new[]. C++ would allow that as well. Only, in C++ you would have to ensure you do a delete[], whereas in Java, you would not have to (in fact, you would be unable to).

    There are also style issues in your code that would go against the grain of most folks who've written C++ for a while, but I will not bring them up now.

    - Anand
    4 years ago
    Thanks Paul. That explains quite a bit. I am responding this late because I am trying to find information on the +1, Thumbs Up, cows, and other similar incentives. Guess this is not a FAQ (since I was unable to find anything there), so is there a web-page that explains all these incentives?

    As one can see, I've been in the ranch for a while, but I don't post very often. One of the mods presented me with the Rancher title well before these incentives were set up (~August 2010).

    - Anand Hariharan


    I found this:
    4 years ago
    Many thanks to the folks at JavaRanch for doing the book promotion and picking me as one of the winners this time.

    - Anand
    4 years ago

    Al Sweigart wrote:Yes. In my opinion, Python is the most readable language I've encountered. Not to start a language war, but I would say it is generally more readable than JavaScript, Java, and Perl.

    In my opinion, Visual Basic is the most readable language. True, it has a "vendor lock-in" (and has its fair share of quirks, but then which language doesn't?) but there are no confusing types such as lists vs tuples vs dictionaries that are different only by virtue of bracket vs paren vs squiggly for their syntax.
    4 years ago

    Al Sweigart wrote:My preference for C++ ternary operators is probably just an old habit. But I do like how it reads "this-condition, this-value-if-true, this-value-if-false" instead of Python's "this-value, if-this-condition-is-true, or-else-this-value-if-the-previous-condition-was-false"

    Would you apply the same reasoning on list comprehensions? They sure read unusually when compared to the explicit for-loop counterparts.
    4 years ago

    Al Sweigart wrote:Anand's comments cover a lot of it.

    Thank you Al. Appreciate it.

    Al Sweigart wrote:Though I wish the ternary operator was more like Java or C++.

    Could you elaborate, please? I suppose opinions vary, but in this case, I know very, very little of Python, and you are ... well you! So opinion matters.

    In C++, one could write

    - whereas in Python one would write

    To me, the latter reads much better. I suppose there are operator precedence issues that come in play but I do not know the language well enough (read 'at all'!).

    thank you,
    - Anand
    4 years ago

    Campbell Ritchie wrote:That I think is C++ code because of the bool keyword, not C.

    C introduced bool albeit after C++. It is a 'typedef' and one needs to include <stdbool.h> for it to be usable.

    Campbell Ritchie wrote:I presume the == and != operators are overloaded on Strings.

    Actually, C does not have a native string type. In C, one uses an array of characters terminated by the NUL character to hold strings. In C++, one has the string class that comes with member functions (substr is one of them) and overloaded operators.

    Campbell Ritchie wrote:I suspect the greatest overhead is in the format time function. What complexity does that run in?

    The code posted by the OP does not need any while loops. Also, the OP could use a function that does the reverse of 'format_time' (i.e., one that takes the formatted string representation of the time and returns the integral equivalent.
    4 years ago

    Alaa Nassef wrote:I took a look in the amazon sample, and it says that the code in the book is using python 3, and might not run correctly, if at all, on python 2.

    Thanks Alaa. I had taken a look at the Amazon sample prior to my post. I took another look at it again after your post. I was unable to find it first. Then yet another look. And yes, there was indeed a statement ("warning") in the 'Downloading and Installing Python' section in the kindle edition of the book's preview.

    - Anand
    4 years ago
    While the OP has explicitly directed this to the author, I wonder if I can put in my 2 cents. NB: My opinion would come from the decidedly wrong side of the spectrum (read "giant ignoramus" when it comes to Python)!

  • Indentaion implies block: This was in old Fortran and in some ways in Makefiles! (I loathe Makefiles just for that reason, FWIW). However, it makes code easier to read when the lines in a block (e.g., an if condition or a loop) are required to be indented by the language rules.
  • Terse and yet almost English like syntax: Unlike Java's or C++'s weird syntax for ternary operator, Python's ternary operator is simply beautiful.
  • Community following: Again, this isn't unique to Python, and yet for some reason, this seems to have worked in Python's favor significantly.

  • In addition to answering the OP's question, I'd be interested in hearing Albert's opinions of my views above.
    4 years ago

    Carey Brown wrote:The first thing that would help is stating the requirements clearly.

    Agree 100%

    Carey Brown wrote:You are computing the elapsed time from the start of the week, ie the previous Sunday at 0 a.m..

    No, just short of that. He is trying to establish the time (the 'unsigned long' integral equivalent) corresponding to the start of the week. Agree it is trivial to compute the elapsed time from that.

    Alex Monari wrote:For work reasons I'm required to discuss the efficiency of the function below by manually stepping through the code. I needed some pointers as to how to go about that.

    See if you can come up with your "'unsigned long' integral equivalent" input_time for 'Saturday July 4 23:59:59 2015' and then step through the code. Do you think the function is efficient?
    4 years ago
    From what little I know, Python 2 has a significantly larger user base (e.g., number of installations, LOC of legacy code, ...) than Python 3 and the newer Python is incompatible in some ways.

    I'd like to know if this difference is significant so far as this book is concerned, and if so, which flavor of Python this book will be using and why. I did not find this information in the Amazon page.

    thank you,
    - Anand
    4 years ago