Palindrome C++ Program

In my class, I was assigned the problem of using functions to write a program in C++ to find out whether a given number is a palindrome or not. I wrote some myself, then did a little research myself. I found 2 other problems that people wrote them using "string::const_iterator" in the "for" loop. I don't know what this means, I can't seem to figure it out, but I put it in and it worked. It won't let me manipulate it at all, so I'm just confused. I also wrote it another way, but it have to use other functions, so I couldn't use it. Any help would, recommendations of different ways to write it, or anything is greatly appreciated. This is my program. Thank you!

#include<iostream>
#include<string>
using namespace std;

int main()
{
string num;
cout << "Enter a number: ";
cin >> num;

for (string::const_iterator i = num.begin(), j = num.end();i > j;i++, j--)
{
if (*i != *j)
{
cout << num << " is not a palindrome!" << endl;
return 0;
}
}
cout << num << " is a palindrome!" << endl;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <string>
#include <algorithm>

bool is_palindrome( std::string str )
{
    if( str.size() < 2 ) return true ; // a string with less than two characters is a palindrome

    std::size_t left = 0 ; // first character position from the left
    std::size_t right = str.size() - 1 ; // first character position from the right

    while( left < right )
    {
        if( str[left] != str[right] ) return false ; // mismatch
        ++left ; // move left to the next character from the left
        --right ; // move right to the next character from the right
    }

    return true ; // everything matched
}

// same as above, using a standard algorithm
bool is_palindrome_v2( std::string str )
{
    // read this first: https://cal-linux.com/tutorials/STL.html
    // http://en.cppreference.com/w/cpp/algorithm/equal
    return std::equal( str.begin(), str.begin() + str.size()/2, str.rbegin() ) ;
}

// brute force version
bool is_palindrome_v3( std::string str )
{
    // check if str compares equal to the reverse of str
    return str == std::string( str.rbegin(), str.rend() ) ;
}
Thank you for the help. The links really explained a lot of the questions that I have. I do have one more question though. Why does everyone use "std::" when solving problems like these, instead of just "using namespace std;" at the beginning?
Why does everyone use "std::" when solving problems like these, instead of just "using namespace std;" at the beginning?


Because it's good to be explicit.

When you using namespace std;, you're putting everything in that namespace into scope. This is problematic because the standard namespace contains many identifiers that can be very common words (such as "map", "distance", "begin", "end", etc). If you have more than one identifier with the same name within the same scope, you can get many misleading errors which can take a while to figure out. This clashing behavior is even more prone to happen if you used the using directive in global scope. It's like releasing all the animals in a zoo and then hoping none of them will kill you.
Topic archived. No new replies allowed.