【问题标题】:Char transformation on simple string array on C++C ++上简单字符串数组的字符转换
【发布时间】:2021-10-17 10:18:23
【问题描述】:

请帮助理解我的简单 C++ 代码有什么问题

  #include <iostream>
  #include <cstring>
  #include <string>

  using namespace std;

  int main()
  {
      string s = "qwertyuiopasdfghjklzxcvbnm";
      string match = "w";
   
      int n = s.length();
   
      char char_array[n + 1];
   
      strcpy(char_array, s.c_str());

   
      for (int i = 0; i < n; i++)
          if (match.compare(char_array[i]) == 0) {
              cout << char_array[i]; 
          }
      return 0;
  }

我收到一个错误:

错误:从‘char’到‘const char*’的无效转换[-fpermissive]

请帮忙把这个 char 转换成 *char 并正确比较它们

【问题讨论】:

  • 您正在将字符串(匹配)与字符进行比较。这是不支持的。请与w[0] 进行比较。但我也很困惑,为什么你要把字符串复制到一个 char 数组开始。 for (auto c : s) 可以很好地循环字符(更不用说这看起来像 find 函数可能更合适的东西)。

标签: c++


【解决方案1】:

您给出的示例中有 2 个错误。

错误 1

你已经写了声明:

 char char_array[n + 1]; //since n is not a compile time constant so this is not standard C++

在 C++ 中,数组的大小必须是编译时间常数。所以你不能写这样的代码:

int n = 10;
int arr[n];    //incorrect

正确的写法是:

const int n = 10;
int arr[n];    //correct

错误 2

您正在尝试将char 转换为const char*,正如compare() 方法中的错误所述。

如果您只是想找出给定字符是否出现在 std::string 中,那么有 2 个选项/解决方案(可能更多)。

解决方案 1

 #include <iostream>
  #include <string>

  using namespace std;

  int main()
  {
      string s = "qwertyuiopasdfghjklzxcvbnm";
      char match = 'w';  //use char instead of std::string
   
      for (int i = 0; i < s.length(); i++)
          if (s[i] == match) {
              cout << "element: "<< match <<" found at index: "<<i<<std::endl; 
          }
      return 0;
  }

above 解决方案一所示,您不需要创建单独的数组。

解决方案 2

您可以使用std::string::find 在另一个字符串中查找给定的子字符串。这看起来像:

 #include <iostream>
  #include <string>

  using namespace std;

  int main()
  {
      string s = "qertyuiowpasdfghjklzxcvbnm";
      std::string match = "w"; //using std::string instead of char
   
      std::size_t index = s.find(match);
      
      if(index != std::string::npos)
      {
          std::cout<<"string: "<<match<<" found at index: "<<index<<std::endl;
      }
      else 
      {
          std::cout<<"string: "<<match<<" not found"<<std::endl;
      }
      return 0;
  }

解决方案2的输出可见here

解决方案 3


#include <iostream>
#include <string>
int main()
{
   std::string s = "qertyuiopasdfwghjklzxcvbnm";
   std::size_t index1 = s.find_first_of('w');

   if(index1 != std::string::npos)
   {
        std::cout<<"found at index: "<<index1<<std::endl;
  }
    else 
    {
        std::cout<<"not found"<<std::endl;
        
    }
    return 0;
}

【讨论】:

    【解决方案2】:

    正如错误中所述,您无法将单个字符转换为字符串。但是你可以使用std::string(size_t , char ) 构造函数。

    #include <iostream>
    #include <cstring>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string s = "qwertyuiopasdfghjklzxcvbnm";
        string match = "w";
     
        int n = s.length();
     
        char char_array[n + 1];
     
        strcpy(char_array, s.c_str());
    
     
        for (int i = 0; i < n; i++)
            if (match.compare(string(1, char_array[i])) == 0) {
                cout << char_array[i]; 
            }
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-16
      • 2019-11-05
      • 2017-07-08
      • 2015-07-21
      • 2015-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多