繁体   English   中英

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

[英]What am I doing wrong? (C++ strings)

#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 ++禁止指针和整数之间的比较”

有帮助吗?

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

答:在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;
}

建议:

  • 使用<string>而不是<cstring>
  • 使用输入和字符串时,使用std::getline而不是std::cin来使用整行,
  • std::string::find是比手动循环更好的选择。

任何帮助?

  1. 你不能在对象std :: string的实例上调用c函数strlen()而是使用std :: string :: length()
  2. std :: string的operator []返回类型char,你试图与字符串常量比较,类型为const 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', '\0'}
        //{
            // 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 );
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM