注册 登录
编程论坛 C++教室

帮忙检查一下有没有编译错误

根根本根 发布于 2018-05-31 18:50, 1303 次点击
#include<bits/stdc++.h>
using namespace std;
int main() {
    string s,s2;
    getline(cin,s);
    getline(cin,s2);
    while(s.find(' ')!=string::npos){
        s.erase(s.find(' '),1);
    }
    while(s2.find(' ')!=string::npos){
        s2.erase(s2.find(' '),1);
    }
    cout<<s<<endl<<s2<<endl;
    for(int i=0; i<s.size(); i++) {
        s[i]=toupper(s[i]);
    }
    for(int i=0; i<s2.size(); i++) {
        s2[i]=toupper(s2[i]);
    }
    if(s==s2&&s.size()==s2.size()){
        cout<<"YES";
    }
    else{
        cout<<"NO";
    }
    return 0;
}
4 回复
#2
rjsp2018-06-01 09:17
C++中已有的函数要用起来,不要重复造轮子
比如 删除空格 s1.erase( remove(s1.begin(),s1.end(),' '), s1.end() );
比如 删除空白字符 s1.erase( remove_if(s1.begin(),s1.end(),isspace), s1.end() );
比如 小写转成大写 std::transform( s1.begin(), s1.end(), s1.begin(), toupper );

以上皆小事,大事是 你这道题根本不需要去修改string,你没有想到正确的算法。
程序代码:
#include <string>
#include <algorithm>

bool equal_ignore_space_case( const std::string& a, const std::string& b )
{
    size_t i=0, j=0;
    for( ; i=a.find_first_not_of(" ",i), j=b.find_first_not_of(" ",j)
         , i!=std::string::npos && j!=std::string::npos && toupper(a[i])==toupper(b[j])
         ; ++i,++j );
    return i==std::string::npos && j==std::string::npos;
}

#include <iostream>
using namespace std;

int main( void )
{
    string s1, s2;
    getline( cin, s1 );
    getline( cin, s2 );
    cout << (equal_ignore_space_case(s1,s2)?"YES":"NO") << endl;
}

#3
根根本根2018-06-21 15:21
回复 2楼 rjsp
谢谢
#4
cstdio2018-07-18 14:20
#include<bits/stdc++.h>报错没有该文件
#5
rjsp2018-07-18 14:34
以下是引用cstdio在2018-7-18 14:20:09的发言:

#include报错没有该文件
非标准,而是 g++ 特有的
所以每次看到有人用 #include<bits/stdc++.h> 都像吞了苍蝇一样恶心
1