【问题标题】:Recursion to remove duplicate characters c++递归删除重复字符c ++
【发布时间】:2022-01-26 21:03:18
【问题描述】:
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

void rmvdupli(string s)
{
    if (s.length() == 1)
    {
        cout << s;
        return;
    }
    char c = s.at(0);
    if (((s.substr(1)).find(c)) >= 0)
        cout << "";
    else
        cout << c;
    rmvdupli(s.substr(1));
}

int main()
{
    cout << "Enter the string " << endl;
    string s;
    cin >> s;
    rmvdupli(s);
    return 0;
}

输出

输入字符串

ababcdc

c

代码有什么问题?看起来完全没问题,但没有答案!

【问题讨论】:

  • string::find 返回一个无符号整数类型的值。它总是 >= 0;它在物理上是不可能的。
  • 尝试较小的测试用例,例如空字符串、单个字符或两个字符(唯一或重复)。然后,在调试器中运行你的代码(尝试找到一个视频教程!)这应该很容易找到错误。

标签: c++ string recursion duplicates


【解决方案1】:

下面一行:

if (((s.substr(1)).find(c)) >= 0)

应该改为:

if (s.substr(1).find(c) != std::string::npos)

基本上就是说c是在s.substr(1)中找到的。

如果找不到字符,find 返回std::string::npos

【讨论】:

    【解决方案2】:

    我已经尝试解决你的这个问题。

    我所做的是,我用空格替换了重复的字符。

    如果您想删除空格,您可以在互联网上轻松找到代码。

    C++ 代码:

    #include <iostream>
    #include <string>
    #include <algorithm>
    using namespace std;
    
    void RemoveDuplicateChar(string inputString)
    {
        if(inputString.length() == 1){
            cout << inputString;
            cout << "\nIt has only one character";
        }else{
           int  stringLength = inputString.length();
           cout<< "Original string: ";
           cout << inputString;
           cout << "\n";
                for(int i =0 ; i<stringLength; i++){
                    for(int j = i+1; j< stringLength; j++){
                        if(inputString[i] == inputString[j]){
                            inputString[j] = ' ';
                            
                        }else{
                            continue;
                        }
                    }
                }
                cout << "Duplicate character removed\n";
                cout << "New String: ";
                cout << inputString;
                
                
                
            
        }
    }
    
    int main()
    {
        string input;
        cout << "Enter a string with repeated characters: ";
        cin >> input;
        RemoveDuplicateChar(input);
        
    
        return 0;
    }
    

    输出:

    输入一个重复字符的字符串:ababcdc

    原字符串:ababcdc

    删除重复字符

    新字符串:ab cd

    【讨论】:

      【解决方案3】:

      我对您的代码进行了一些重构,以修复错误并提高其可读性和性能。我还添加了另一个重载函数,它明显更快并且使用 0 堆分配。虽然它做同样的事情,但以一种更优化的方式。

      这里:

      #include <iostream>
      #include <string>
      #include <string_view>
      
      
      // this is basically your function but is more efficient now
      void removeDuplicateChars( const std::string& str )
      {
          if ( str.length( ) == 1 )
          {
              std::cout << str;
      
              return;
          }
      
          const char firstChar = str[ 0 ];
          const std::string&& restOfStr { str.substr( 1 ) };
      
          if ( restOfStr.find( firstChar ) == std::string::npos )
          {
              std::cout << firstChar;
          }
          else
          {
              std::cout << "";
          }
      
          removeDuplicateChars( restOfStr );
      }
      
      // this one is far superior to the above one
      void removeDuplicateChars( std::string_view&& strView )
      {
          if ( strView.length( ) == 1 )
          {
              std::cout << strView;
      
              return;
          }
      
          const char firstChar = strView[ 0 ];
          strView.remove_prefix( 1 );
      
          if ( strView.find( firstChar ) == std::string_view::npos )
          {
              std::cout << firstChar;
          }
          else
          {
              std::cout << "";
          }
      
          removeDuplicateChars( std::move( strView ) );
      }
      
      
      int main( )
      {
          std::cout << "Enter the string: ";
          std::string str;
          std::cin >> str;
          removeDuplicateChars( str ); // this calls the first overload
      
          std::cout << '\n';
      
          removeDuplicateChars( std::string_view( "ababcdc" ) ); // this calls the
                                                                 // second overload
      
          return 0;
      }
      

      示例输入/输出:

      Enter the string: ababcdc
      abdc
      abdc
      

      我强烈建议您放弃第一个重载,只使用第二个。它不仅速度更快,而且还支持std::stringstd::string_viewC 风格的字符串类型

      这里有一些有用的链接可以提高你的知识:
      std::string_view
      std::string::substr
      std::string::find

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-12
        • 2023-01-30
        • 2016-08-15
        • 1970-01-01
        • 2018-05-01
        • 2016-02-12
        • 2021-10-04
        • 2012-02-27
        相关资源
        最近更新 更多