From 7a1a7927c9953027694e9152c1fe43a647be7b50 Mon Sep 17 00:00:00 2001 From: GJBR <599398713@qq.com> Date: Tue, 12 Aug 2025 09:27:50 +0000 Subject: [PATCH] =?UTF-8?q?add=20C++/TypeCastError.cpp.=20=E5=8F=8D?= =?UTF-8?q?=E5=BA=8Ffor=E5=BE=AA=E7=8E=AF=E9=81=8D=E5=8E=86=EF=BC=8C?= =?UTF-8?q?=E8=B8=A9=E5=9D=91=E5=AE=9E=E6=93=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: GJBR <599398713@qq.com> --- C++/TypeCastError.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 C++/TypeCastError.cpp diff --git a/C++/TypeCastError.cpp b/C++/TypeCastError.cpp new file mode 100644 index 0000000..0790d00 --- /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 -- Gitee