Timeline for Close to the metal C++ Palindrome Program
Current License: CC BY-SA 3.0
17 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Apr 22, 2017 at 23:34 | history | edited | user136614 | CC BY-SA 3.0 |
Updated links and end code.
|
Apr 22, 2017 at 23:33 | comment | added | user136614 | @Davislor That is true, making that adjustment now | |
Apr 22, 2017 at 23:32 | comment | added | user136614 | @RailsKiddie Assignment and operation in the for loop result is constant, so a nice compiler will optimise it for you. And there won't be any side effects, because constant expression. | |
Apr 22, 2017 at 18:17 | comment | added | Davislor |
One minor improvement you can make:: if num_to_parse is 1, then num_to_parse/2 is 0, so you could remove the check that num_to_parse == 1 and change the loop condition to i < num_to_parse/2 . The middle letter of an odd-numbered string is always equal to itself and does not need to be checked. That way, when the length is 1, the program just falls through the loop.
|
|
Apr 22, 2017 at 18:04 | comment | added | Angular4 Kiddie |
Interesting point. So we're talking about a problem that would only be applicable to single operation per cycle environments? Do you foresee any problems with taking the operation and assignment out of the for loop which might cause side-effects of some sort?
|
|
Apr 22, 2017 at 18:02 | comment | added | Angular4 Kiddie | @Incomputable surely it's fairly obvious that making an assignment and performing an operation every single time the loop executes, where it might have been performed once before the loop would result in increased load under most conceivable circumstances . I mean, am I being absurd here? | |
Apr 22, 2017 at 17:18 | comment | added | Angular4 Kiddie | I probably don't understand this properly, but won't performing the assignment and operation within the for loop result in slower code under some conditions? | |
Apr 22, 2017 at 15:33 | comment | added | user136614 | @pacmaninbw A few reasons: (1) More people are familiar and comfortable with indices, including myself. (2) There's not much benefit to using iterators. (3) If the string has an even length, the iterators will "cross over" and iterate over the entire string, rather than only half. (4) I have to remember to decrement the end iterator before starting, because it points to the element after the end. | |
Apr 22, 2017 at 15:19 | comment | added | pacmaninbw♦ | I'm curious, why didn't you rewrite it using iterators rather than indexing? leftChar = inputString.begin() rightChar = inputString.end() while (leftchar != rightChar) { ... } | |
Apr 22, 2017 at 14:48 | comment | added | user136614 | I can say I've read it now :D | |
Apr 22, 2017 at 14:44 | comment | added | anon | Yeah, I assumed as much -- that's pretty common here. Like I said, good answer! You might still be interested in How to Answer, if only so you can say you read it. | |
Apr 22, 2017 at 14:41 | comment | added | user136614 | @RailsKiddie tested it with some print-debugging, and we were getting negative array indices. NEGATIVE INDICES! | |
Apr 22, 2017 at 14:40 | comment | added | Angular4 Kiddie | @InternetAussie Yeah, it just happened in the coding environment I was using it did work, hence posting here, but I've since found it doesn't work in most others. | |
Apr 22, 2017 at 14:37 | comment | added | user136614 |
Thank you, thank you :) The truth is, I started writing this before you submitted it, so we both did these things ourselves :P But then the iffy if statement struck me, so I tested it and had to rewrite because OP's code didn't work.
|
|
Apr 22, 2017 at 14:37 | review | First posts | |||
Apr 22, 2017 at 15:05 | |||||
Apr 22, 2017 at 14:35 | comment | added | anon | Nice first answer! I'm gonna pretend you stole all the things we both noticed from me, even though that's probably not true at all. :) | |
Apr 22, 2017 at 14:32 | history | answered | user136614 | CC BY-SA 3.0 |