C++ C++;字符串大小\u t

C++ C++;字符串大小\u t,c++,g++,runtime-error,size-t,C++,G++,Runtime Error,Size T,下面的代码按预期工作,也就是说,它以相反的方式打印字符串 std::string s("abcd"); for (int i=s.length()-1; i>=0; --i) { std::cout << s[i]; } 无符号类型永远不会有负值。因此,有条件的 i>=0将始终计算为true这是因为std::size\t无法存储负数,因此条件始终为true也许您的意思是unsignedGCC可以告诉您:警告:无符号表达式的比较>=0始终为true[-Wtype li

下面的代码按预期工作,也就是说,它以相反的方式打印字符串

std::string s("abcd");

for (int i=s.length()-1; i>=0; --i) {
    std::cout << s[i];
}

无符号类型永远不会有负值。因此,有条件的
i>=0
将始终计算为
true
这是因为
std::size\t
无法存储负数,因此条件始终为true

也许您的意思是unsignedGCC可以告诉您:警告:无符号表达式的比较>=0始终为true[-Wtype limits]
std::string s("abcd");

for (std::size_t i=s.length()-1; i>=0; --i) {
    std::cout << s[i];
}
C:/MinGW/bin/g++ --version
g++ (GCC) 6.1.0