简体   繁体   English

有关C ++字符串的问题

[英]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 ? 在上面的代码中,即使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/ 新代码来自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. 有多个isdigitispunctisspace函数-在<ctype.h>头文件的全局命名空间中,在<cctype><clocale>头文件的std命名空间中有多个。 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. 为了使用std::string类,您需要使用<string>而不是<string.h>

Assuming test is a Palindrome object, then test.Palindrome::isPalindrome() should be just test.isPalindrome() . 假设testPalindrome对象,则test.Palindrome::isPalindrome()应该只是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. 如果省略Palindrome构造函数,则phrase成员保持空白,并且isPalindrome()实现对空白phraselength为0)返回true ,因为for循环无需检查。 Which is technically correct - a blank string is a palindrome. 从技术上讲这是正确的-空字符串是回文。

The :: indicates that you're using isdigit and the others from the global namespace. ::表示您正在使用isdigit ,而其他人则使用全局命名空间。 The isdigit is a part of other header files, such as <ctype.h> . isdigit是其他头文件的一部分,例如<ctype.h>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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