[英]Why do I have an infinite loop? Simple question, short code
If the input array is empty, then array.size()
should be 0. The first for
, that goes from 0
to array.size() - 1
, should mean it goes from 0
to -1
, right?如果输入数组为空,则
array.size()
应该为 0。第一个for
,从0
到array.size() - 1
,应该意味着它从0
到-1
,对吗?
This for
then, should not be entered and the function should return the inversionsCounter value which would be 0
这时候
for
不应该输入并且 function 应该返回 inversionsCounter 值,这将是0
But this doesn't happen, and the code enters an infinite loop.但这并没有发生,代码进入了死循环。 Why is this?
为什么是这样?
Here is the code:这是代码:
#include <vector>
#include <iostream>
using namespace std;
int countInversions(vector<int> array)
{
int inversionsCounter = 0;
for (int i = 0; i < array.size() - 1; ++i)
for (int j = i + 1; j < array.size(); ++j)
if (array[i] > array[j])
++inversionsCounter;
return inversionsCounter;
}
int main()
{
vector<int> array = {};
cout << array.size();
cout << countInversions(array);
}
The type of the return value of the member function size
of the class template std::vector
is unsigned integer type usually equivalent to the type size_t
. class 模板
std::vector
的成员 function size
的返回值类型为无符号 integer 类型,通常等同于类型size_t
。
So in the condition of the for loop所以在for循环的条件下
for (int i = 0; i < array.size() - 1; ++i)
due to the usual arithmetic conversions the both operands of the expression由于通常的算术转换,表达式的两个操作数
i < array.size() - 1
are converted to the unsigned integer type that corresponds to the type std::vector<int>::size_type
.转换为对应于类型
std::vector<int>::size_type
的无符号 integer 类型。 As a result the expression array.size() - 1
is converted to a maximum value of the type when the member function size
returns 0.结果,当成员 function
size
返回 0 时,表达式array.size() - 1
被转换为该类型的最大值。
Here is a demonstrative program.这是一个演示程序。
#include <iostream>
#include <iomanip>
int main()
{
std::cout << size_t( -1 ) << '\n';
std::cout << std::boolalpha << ( 0 < size_t( -1 ) ) << '\n';
return 0;
}
Its output is它的 output 是
18446744073709551615
true
So when the member function size
returns 0 you have in fact a loop like this因此,当成员 function
size
返回 0 时,您实际上有一个这样的循环
for (int i = 0; i < 18446744073709551615; ++i)
Instead of the type int
you should use at least the type size_t
for indices.而不是
int
类型,您应该至少使用size_t
类型作为索引。 And the outer loop should look like外循环应该看起来像
for ( size_t i = 0; i < array.size(); ++i )
The return type for the size() method on vector<> is size_t
, an unsigned integral value - often just unsigned int
. vector<> 上的 size() 方法的返回类型是
size_t
,一个无符号整数值——通常只是unsigned int
。 This type, size_t
, is unsigned and can not carry negative numbers.这种类型
size_t
是无符号的,不能携带负数。 In most cases the compiler will warn you about this.在大多数情况下,编译器会就此警告您。
When it is time to execute the code, however, the compiler substitutes the 'ones complement' form of the signed integer -1 (calculated from size() - 1 ) to the size_t
(unsigned) type.但是,当需要执行代码时,编译器会将带符号的 integer -1(从 size() - 1 计算)的“补码”形式替换为
size_t
(无符号)类型。
This -1 signed number is often very very large when an attempt is made to represent it as an un signed number.当试图将它表示为无符号数时,这个 -1 有符号数通常非常大。 Much of the specifics here depend on your particular machine and compiler details.
这里的许多细节取决于您的特定机器和编译器细节。
This means that your outer for loop for (int i = 0; i < array.size() - 1; ++i)
is not running in an infinite loop, but a very long one.这意味着您的外部 for 循环
for (int i = 0; i < array.size() - 1; ++i)
不是在无限循环中运行,而是在一个很长的循环中运行。
cast array.size() to an int otherwise like...将 array.size() 转换为 int 否则就像...
(int)array.size()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.