简体   繁体   中英

Questions regarding C++ string

phrase.erase(remove_if (phrase.begin(), phrase.end(), ::isdigit), phrase.end());

In the above code, why do I have to use :: even though I used using namespace std ?

#include "Palindrome.h"
#include <iostream>
#include <string.h>
#include <algorithm>

using namespace std;

Palindrome::Palindrome (string Phrase){
    phrase=Phrase;
}

void Palindrome::removeNonLetters()
{
    phrase.erase(remove_if (phrase.begin(), phrase.end(), ::isdigit), phrase.end());

    phrase.erase(remove_if (phrase.begin(), phrase.end(), ::ispunct), phrase.end());

    phrase.erase(remove_if (phrase.begin(), phrase.end(), ::isspace), phrase.end());
}

void Palindrome::lowerCase()
{
    for (int i=0; i<phrase.length(); i++)
    {
        phrase[i] = tolower(phrase[i]); 
    }
}

bool Palindrome::isPalindrome()
{
    int length=phrase.length(); 
    int a=0;    
    for (int i=0;i<length/2;i++)
    { 
        if(phrase[i] != phrase[length-a-1])
        {
            return false;
            break;
        }
        a++;
    }
    return true;
}

The above code is to check if the string is a Palindrome. I don't understand why I need to use the first part which is

Palindrome::Palindrome (string Phrase){
    phrase=Phrase;
}

I will always get "yes" if I deleted the above part.

The test code in main is

if(test.Palindrome::isPalindrome() == 1){
    cout<<"Yes"<<endl;
}
else {
    cout<<"No"<<endl;
}

One more question. I try to change the lowercase of above code, I got error. Do anyone know what happen with it? The new code was from https://www.geeksforgeeks.org/conversion-whole-string-uppercase-lowercase-using-stl-c/

Before

 void Palindrome::lowerCase()
{
    for (int i=0; i<phrase.length(); i++)
    {
        phrase[i] = tolower(phrase[i]); 
    }
}

After

void Palindrome::lowerCase(){

transform(phrase.begin(), phrase.end(), phrase.begin, ::tolower);

}

Can anyone explain it to me? Many thanks!

There are multiple isdigit , ispunct , and isspace functions - ones in the global namespace in the <ctype.h> header, and several in the std namespace in the <cctype> and <clocale> headers. Prefixing them with :: says you want to use the ones from the global namespace.

You need to use <string> instead of <string.h> in order to use the std::string class.

Assuming test is a Palindrome object, then test.Palindrome::isPalindrome() should be just test.isPalindrome() .

If you omit the Palindrome constructor, then the phrase member remains blank, and your isPalindrome() implementation returns true for a blank phrase ( length is 0) because there is nothing for the for loop to check. Which is technically correct - a blank string is a palindrome.

The :: indicates that you're using isdigit and the others from the global namespace. The isdigit is a part of other header files, such as <ctype.h> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM