diff --git a/C++/TypeCastError.cpp b/C++/TypeCastError.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0790d00d8693d33f940987447abd21df46316980 --- /dev/null +++ b/C++/TypeCastError.cpp @@ -0,0 +1,15 @@ +#include + +// 反序for循环遍历。auto推导为size_t类型,导致程序出现死循环。 +// 众所周知,size_t是无符号类型,不能小于0。 +// 解决方法:将i的类型改为int,或者使用有符号的整数类型。 + +int main() { + // 反序for循环遍历 + std::vector vec = { 1, 2, 3, 4, 5 }; + for (auto i = vec.size(); i >= 0; i--) + { + printf("i = %d\n", i); + } + return 0; +} \ No newline at end of file