gpt4 book ai didi

c++ - 已签名/未签名函数的警告

转载 作者:行者123 更新时间:2023-11-30 05:47:42 24 4
gpt4 key购买 nike

作为我项目的一部分,我编写了一个函数来解析字符串参数。功能一切正常,但我收到一条警告:C4018: '<' : signed/unsigned mismatch .我不知道为什么会出现此警告。我可以在这里得到一些帮助吗?谢谢!

void FileMgr::parseCmdLineForTextSearch(std::string b)
{
int count=0;
int flag = 0;
patternVector.clear();
for (int i = 0; i < b.length(); i++) // this line where
// the warning line comes


{
if (b[i] == '"')
{
count++;
}
}
if (count == 2)
{
for (int i = 0; i < b.length(); i++)
{
if (b[i+1] == '"')
{
flag = 1;
tmp = b.substr(0, i+1);
tmp.erase(0, 1);
break;
}
else
{
continue;
}
}
std::istringstream iss(b);
std::string word;
while (iss >> word)
{ // for each word in b
if (word.find("*.") == 0)
{ // if it starts with *.
patternVector.push_back(word); // add it
}
}
if (patternVector.size() == 0)
{
patternVector.push_back("*.*");
}
isCorrect = true;
}
else
isCorrect = false;
}

最佳答案

b.length()返回未签名的 size_t。在您的 for 循环中,您正在比较带符号的 int i 和无符号的 b.length(),这就是您看到警告的原因。要摆脱它,请在使用 i 指示数组索引时使用 size_t i 而不是 int i

不相关:您在此处有越界访问 if (b[i+1] == '"')

关于c++ - 已签名/未签名函数的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28506340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com