Nice post. Although I would have done some things different, the idea
is good.
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
What would you have done differently? I am open to any and all suggestions.
NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
Why have you hard coded the numbers 5 and 4?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
I hard coded them just to show that you can use a vector as well. It was just an example.
NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
Instead of using two for-loops to load the vector with data, you could also do:
int arr[] = {1,2,3,2,1};
vector<int> number(arr, arr + 5);
Just a tip though.
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
Thanks Nick I did not know a vector could be inialized that way..
NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
What would you have done differently? I am open to any and all suggestions.
Well you can make your code more concise.
template<typename StartItr, typename EndItr>
bool isPalin(StartItr start,EndItr end){
assert(start <= end);
if(start == --end) return true;
while(start < end && *start++ == *end--) continue;
return (*--start == *++end);
}
But one might be able to program this using metaprograming technique, and
be able to find out whether an input is a palindrome during compile time.
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
You pass num1 which is a vector of ints, and you pass its start position and its end
position, thats redundant. Makes the user do extra code. And you can't print a vector
using operator<< without defining the operator<<.
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
You are right with that arkoenig. I didn't realize that <= wasn't supported by bidirectional iterators.
NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189