我做错了什么?(C++字符串)

What am I doing wrong? (C++ strings)

本文关键字:C++ 字符串 错了 什么      更新时间:2023-10-16
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    int n;
    cin >> n;
    string a;
    cin >> a;
    int index;
    for(int i=0;i<strlen(a);i++)
    {
        if(a[i]=="?")
        {
            index=i;
        }
    }
    cout << index;
    return 0;
}

我想在字符串中找到"?"(如果有的话(,但我得到了错误:"ISO C++禁止指针和整数之间的比较">

有什么帮助吗?

在"?"周围使用单引号字符,表示字符而不是字符串。这就是为什么你的比较失败的原因。

A:在for循环中混合C++字符串和旧的C风格(strlen(函数(改用a.length()(

B: 将C字符串与字符if(a[i]=="?")进行比较应该是if(a[i]=='?')(使用单引号比较字符-双引号使其成为字符串比较,实际上是指针比较,不会执行您期望的操作(

除了其他答案外,您的程序还可以通过使用一些方便的内置函数简化为以下内容:

#include <iostream>
#include <string>
int main()
{
    std::string a;
    std::getline(std::cin, a);
    int index;
    auto pos = a.find("?");
    if (pos != std::string::npos)
      index = pos;
   else
      index = -1;
    std::cout << index;
}

忠告:

  • 用CCD_ 4代替CCD_
  • 当使用输入和字符串时,使用std::getline而不是std::cin来消耗整行
  • std::string::find是比手动循环更好的选择

有什么帮助吗?

  1. 不能对对象的实例调用c函数strlen((std::string使用std::string::length((
  2. std::string的operator[]返回要与类型为const char的字符串常量进行比较的char类型*
  3. 你使用了未初始化的变量索引,如果你的条件从未成功(假设你通过事件修复了它(,你就没有办法找到它
  4. std::string有find((方法,如果找不到,则返回字符串或常量std::string::npos中的位置

内联注释。。。

#include <iostream>
//#include <cstring> // You don't need this
#include <string> // You do need this
// using namespace std; // You don't need this
int main()
{
    // int n; // This is not used
    // cin >> n; // Nor is this
    std::string user_input; // Use std:: and meaningful variable names
    std::cin >> user_input; // "
    int index = user_input.find('?'); // This is the simplest way to find a character
    // for(int i=0;i<strlen(a);i++) // strlen() does not work here
    // {
    //  if(a[i]=="?") // chars are quoted with single quotes, as above. This is {'a', ''}
        //{
            // index=i; You could break here, too, otherwise you'll reurn the last '?'
        //}
    // }
    std::cout << index;
    // return 0; // This is unnecessary
}

"?"在内存中创建一个字符串。像"?"这样的常量字符串将指向存储器中地址的开头。因此,它是一个指针。

'?'在适当的位置创建单个字符,并且不创建指针。因此,当与另一个字符或整数进行比较时,ISO C++禁止将整数(或字符(与指针(例如字符串(进行比较。

所以应该是

    if(a[i]=='?')
    {
        index=i;
    }

在这里,我将演示如何从std::string中找到一个*问号('?'(:请务必阅读评论!

int main( void )
{
    // DECLARE LOCAL VARIABLES
    // Declare strA as std::string
    std::string strA = "";
    // Declare nIndex as int. We set the value of it to -1 to let us know
    // if there were any '?' within strA
    int nIndex = -1;
    // INITIALIZE
    // Set the value of strA to the line that was inputted
    // You might want to loop this until the length of strA
    // is greater than 0
    std::getline( std::cin, strA );
    // SEARCH FOR THE FIRST '?'
    // if std::string::find returns -1, then no '?' was found within strA
    nIndex = strA.find( '?' );
    // CHECKING AND PRINTING nIndex
    // Remember why strA is -1?
    if( nIndex == -1 )
        std::cout << "The inputted string does not contain any '?'...n";
    else std::cout << "The first '?' is found at: " << nIndex;
    // DONE
#ifdef DEBUG
    std::cout << "nDebugging > Paused! Enter any key to continue...n";
    ::getchar( );
#endif // DEBUG
    return( 0 );
};