// if (var1.substr (0, 2) == alphaLetter[i])


if不合法。是否可以通过某种方式检查var1中的前三个字符是否在alphaLetter中?

int main ()
{
    std::string const alphaLetter = "ABCDEFGHIJKLMNOPRSTVUYWQZX";

    std::string var1= "";
    std::cout << " Enter 6 characters: ";
    std::cin >> var1;
    for (int i = 0; i < alphaLetter.size (); i++)
    {
        for (int n = 0; n < alphaLetter.size(); n++)
        {
            if (var1.substr (0, 2) == alphaLetter[i])
            {
                std::cout << "True";
            }
        }
    }
}

最佳答案

if (var1.substr(0, 3).find_first_not_of(alphaLetter) == std::string::npos) {
  // The first three characters are all present in alphaLetter
}

关于c++ - 查找和比较两个字符串中的字符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57950058/

10-15 12:23