提问者:小点点

在C++中使用字符串字符比较时获取错误[closed]


我对C++中的“!=”运算符有问题。我已经对函数中出现这个错误的区域进行了注释,但我找不到解决这个问题的方法。 谁能告诉我这里还有什么可以用的?

int lengthOfLongestSubstring(string s) {
    int n = s.size();
    string unique[27];
    int length = 0;
    for (int i = 0; i <27; i++)
    {
        unique[i] = "-1";
    }

    for (int i = 0; i <n; i++)
    {
        for (int j = 0; j <27; j++)
        {
            if (unique[j] != s[i]) //getting error here 
            {
                if (unique[j] == "-1")
                {
                    unique[j] = s[i];
                    length++;
                }
            }
            else
            {
                return length;
            }
        }
    }
    return length;
}
void main()
{
    string s = "wilnmmmp";
    int n = lengthOfLongestSubstring(s);
    cout << n << endl;
    system("pause");
}

共1个答案

匿名用户

您正在尝试将字符串数组内容(唯一)与字符串的字符进行比较。 另外,如果您将唯一数组更改为char,它仍然会给出一个错误,因为“-1”是一个多字符,请尝试分配类似“0”或一个正数,并将唯一数组更改为char。 为了方便。 你可以看看下面我的代码。 我的实现

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(c++|中使|字符串|字符|获取|closed)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?