C++ 输入字符串以查找回文之前的输出

C++ 输入字符串以查找回文之前的输出,c++,getline,palindrome,stdstring,C++,Getline,Palindrome,Stdstring,该代码在输入测试用例的值后给出了YES输出。 代码:用于字母数字pallindrome int main() { int t; cin>>t; while(t--){ string s; int count = 0,size = 0; getline(cin,s); cout<<s<<endl; s.erase(remove_if(s.begin(),s.e

该代码在输入测试用例的值后给出了YES输出。 代码:用于字母数字pallindrome

int main() {
    int t;
    cin>>t;
    while(t--){
        string s;
        int count = 0,size = 0;
        getline(cin,s);
        cout<<s<<endl;
        s.erase(remove_if(s.begin(),s.end(),not1(ptr_fun((int(*)(int))isalnum))), s.end());
        for(int i=0;i<=s.size()/2;i++){
            size++;
            if(tolower(s[i])==tolower(s[s.size()-i-1])){
                count++;
            }
            else
                break;
        }
        if (count==size)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}
该代码在输入测试值后给出“是”输出 案例我得到的输出是YES,没有输入任何字符串

你的问题是:

/* code */
    cin>>t;    -----------> std::cin        
    while(t--)
    {
        string s;
        int count = 0,size = 0;
        getline(cin,s); ------------> std::getline()

/* remaining code */
使用
std::cin
之类的内容进行读取会在输入流中留下换行符。当控制流达到std::getline()时,换行符将被丢弃,但输入将立即停止。这导致,
std::getline()
s尝试读取新行并跳过输入

修复:当从以空格分隔的输入切换到以换行符分隔的输入时,您希望通过执行
std::cin.ignore()

固定代码应为:

#包括
#包括
#包括
#包括
int main()
{
int t;
标准:cin>>t;
//修理
std::cin.ignore(std::numeric_limits::max(),'\n');
而(t--)
{
std::字符串s;
整数计数=0,大小=0;
getline(标准::cin,s);
/*剩余代码*/
}

您似乎缺少一些标题并拼错了某些类型(假设
string
应该是
std::string
tolower
应该是
std::tolower
,等等)我使用了namespace std,所以std没有任何用处::你真的应该避免使用namespace std-这是一个坏习惯,而且当你不期望它的时候。习惯使用namespace前缀(
std
故意很短),或者只将您需要的名称导入到最小的合理范围内。到目前为止,这并没有给我带来任何问题,我已经得到了下面提到的问题的解决方案。似乎std在这里不是问题。
#include <iostream>
#include <string>
#include <limits>
#include <algorithm>

int main()
{
    int t;
    std::cin >> t;
    // fix
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    while(t--)
    {
        std::string s;
        int count = 0,size = 0;
        getline(std::cin,s);
        /* remaining code */
}