【问题标题】:really odd problems in palindrome program [duplicate]回文程序中非常奇怪的问题[重复]
【发布时间】:2016-06-01 00:00:14
【问题描述】:

我一定要抽几个字,测试一下是不是回文,不算空格,“,”和“-”的字符。

      #include <iostream>
    #include <algorithm>
    #include <string>
    using namespace std;
    int main() 
    {
       int inputs;
       for (int i=0;i<inputs;i++)
      {string str;
      getline(cin, str);
      transform(str.begin(), str.end(), str.begin(), ::tolower);
       str.erase(remove_if(str.begin(),str.end(),','),str.end());
       str.erase(remove_if(str.begin(),str.end(),'-'),str.end());
       str.erase(remove_if(str.begin(),str.end(),' '),str.end());
      if (str == string(str.rbegin(), str.rend()))
      cout << "Y ";
      else 
      cout << "N "; }

}  

问题: 在 /usr/include/c++/4.9/bits/stl_algobase.h:71:0、/usr/include/c++/4.9/bits/char_traits.h:39、/usr/include/c++/4.9 中包含的文件中/ios:40,来自 /usr/include/c++/4.9/ostream:38,来自 /usr/include/c++/4.9/iostream:39,来自 solution.cc:1:/usr/include/c++/4.9/bits /predefined_ops.h:在 'bool __gnu_cxx::__ops::_Iter_pred<_predicate>::operator()(_Iterator) 的实例化中 [with _Iterator = __gnu_cxx::__normal_iterator >; _Predicate = char]':/usr/include/c++/4.9/bits/stl_algo.h:866:28: 来自 '_ForwardIterator std::__remove_if(_ForwardIterator, _ForwardIterator, _Predicate) [with _ForwardIterator = __gnu_cxx::__normal_iterator >; _Predicate = __gnu_cxx::__ops::_Iter_pred]'/usr/include/c++/4.9/bits/stl_algo.h:937:47: 来自'_FIter std::remove_if(_FIter, _FIter, _Predicate) [with _FIter = __gnu_cxx ::__normal_iterator >; _Predicate = char]'solution.cc:12:49: 这里需要/usr/include/c++/4.9/bits/predefined_ops.h:231:30: 错误:表达式不能用作函数{ return bool(_M_pred( *__它)); } ^

(对我来说完全是胡言乱语。) 有什么解决办法吗?

【问题讨论】:

  • 您是在测试每个单词还是单词序列。我不清楚边界是什么。
  • 如果您真的想在 C++ 中执行此操作并使用算法,这是一个使用 std::reversestd::remove_if 的 5 或 6 行程序。
  • 它怎么可能永远不是回文 -- 当第一个字符'A'不等于时,第一个输入怎么可能是回文最后一个字符'a',并且您的代码中没有任何地方将字母转换为相同的大小写(全部大写或全部小写)?那是一个信号,你应该调试你的代码。此外,为了使字母大小写相同,缺少一整块代码。
  • 提醒:在 C++ 中,字母区分大小写。这意味着 'A' != 'a'。

标签: c++


【解决方案1】:

检查回文可以很简单

#include <iostream>
#include <algorithm>
#include <string>

int main() {

  std::string str, rStr;

  std::cout << "Enter String :\n";
  std::cin >> str;

  std::transform(str.begin(), str.end(), str.begin(), ::tolower);
  rStr = str;
  std::reverse(begin(str), end(str));

  (rStr == str) ? std::cout << "Word is palindrone\n" : std::cout << "Word is not palindrone\n";

  return 0;
}

随意修改。

要获得多个单词作为输入,请使用getline(cin, str);

【讨论】:

    【解决方案2】:

    不妨展示一个与您的尝试形成鲜明对比的解决方案:

    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <cctype>
    
    int main() 
    {
        std::string orig;
        while (std::getline(std::cin, orig))
        {
            std::transform(orig.begin(), orig.end(), orig.begin(), ::toupper);
    
            // erase the punctuation and spaces
            orig.erase(std::remove_if(orig.begin(), orig.end(), [](char ch) 
            { return ::isspace(ch) || ::ispunct(ch);}), orig.end()); 
    
            std::cout << ((orig == std::string(orig.rbegin(), orig.rend()))?"Y":"N");
        }
    }
    

    注意std::reverse 不是必需的,使用erase / remove_if 成语删除空格和标点符号。

    【讨论】:

    • 我真的很想给你一个 +1 但using namespace std is poor practice
    • @Serdalis 如果在像这个一样小的模块中使用,这不是坏习惯。在头文件中使用它是一种不好的做法,其中包含头文件会无意中引入您不想要的东西。
    • 当人们将它包含在 C++ 源文件的顶部时,我仍然看到它会咬人。主要是小型辅助功能等。只是我对宠物的厌恶和离题,这个答案仍然很棒。 (现在 +1)。
    • @Serdalis 没问题。我还是改了代码。
    • 问号有什么作用?
    猜你喜欢
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多