【发布时间】:2021-08-02 10:47:30
【问题描述】:
这段代码:
int findsmallerNumber(int low, int high, string *str)
{
int counter = 0;
string ss = *str;
for(int i = low + 1; i <= high; i++)
{
if(ss[i] < ss[low])
counter++;
}
cout<<counter<<" ";
return counter;
}
产生正确的输出为:
4 4 3 1 1 0
但是当计数器变量未初始化时:
int counter;
得到的输出是:
4 8 11 12 13 13
有人可以解释一下这种行为吗?
【问题讨论】:
-
顺便说一句,当
counter是0时,不清楚正确代码的输出如何以4 ...开头 -
@463035818_is_not_a_number 很抱歉 cout 不应该在那里:(。现在已经编辑了。
标签: c++ c++11 local-variables