visual c++忽略if语句
visual C++ ignores if statement
有时visualc++会忽略if条件而不输入它!在调试模式下,我看到条件表达式的值为真,但c++忽略了它!
:
if (childrens.at(j)->rules.size() > tempMax) {
tempMax = childrens.at(j)->rules.size();
}
当我把它改成这样时,它就能正常工作了:
tempInt = childrens.at(j)->rules.size();
if (tempInt > tempMax) {
tempMax = tempInt;
}
为什么?修复方法是什么?
将else添加到if:
if (childrens.at(j)->rules.size() > tempMax) {
tempMax = childrens.at(j)->rules.size();
}else{
string inputCheck;
cout<<"children."<<j<<".rules."<<childrens.at(j)->rules.size()<<" > "<<tempMax;
cout<<"nMy if - statement isn't working (Press Enter/Return to continue): ";
cin.ignore(256,'n');
getline(cin,inputCheck);
}
@user4581301告诉问题,我检查了一下,他是对的。
在这个代码中
if (childrens.at(j)->rules.size() > tempMax) {
tempMax = childrens.at(j)->rules.size();
}
rule.size()
返回unsigned,且tempMax
为int
。它的初始值是INT_MIN
,优化器忽略它,因为unsigned int。总是大于负,但进入后,如果身体tempMax
变成正的,我不知道为什么优化器忽略它!
我写了这个新程序
int _tmain(int argc, _TCHAR* argv[])
{
vector<bool> a;
for (size_t i = 0; i < 20; i++)
{
a.push_back(true);
}
int tempMax = INT_MIN;
for (size_t i = 0; i < a.size(); i++)
{
if (a.size() > tempMax)
{
tempMax = a.size();
}
}
cout << tempMax << endl;
return 0;
}
和输出 -2147483648
,将a.size()
的类型更改为(int)a.size()
后,它固定:
int _tmain(int argc, _TCHAR* argv[])
{
vector<bool> a;
for (size_t i = 0; i < 20; i++)
{
a.push_back(true);
}
int tempMax = INT_MIN;
for (size_t i = 0; i < a.size(); i++)
{
if ((int)a.size() > tempMax)
{
tempMax = a.size();
}
}
cout << tempMax << endl;
return 0;
}
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 是否可以使用if constexpr删除控制流语句
- 我似乎对if/else的基本语句有问题:/
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 以在Qt中的IF语句中设置时间延迟
- Craps游戏问题,忽略if语句
- c++:定义if语句中的模板
- C++嵌套的 If 语句中,它无法按预期工作
- 如果中的多个语句是否与多个 if 相同?
- C++ If/Else 语句被跳过
- 使用语句if时逻辑的问题
- 如何添加语句if-else
- 如何将语句if(isalpha(c))从c++转换为MIPS ?
- 在if语句if C++中创建对象时出现作用域问题
- if语句-if/else和参数放置C++