c++ - string::length 返回错误值?

标签 c++ string string-length

string str = "abcdef";
cout << str.length() << endl; //6
cout << str.length() - 7 << endl; //4294967295

我不明白为什么 str.length() - 7 返回 4294967295 而不是 -1。

你能帮我解释一下吗?

最佳答案

str.length() 返回一个未签名的 size_t。这个未签名的结果环绕,这就是为什么你得到 4294967295 而不是 -1。

关于c++ - string::length 返回错误值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46137336/

相关文章:

c++ - 提供不确定生命周期 bool 值以在线程之间共享的最简单方法是什么?

c++ - 编译但失败的 C++ std::vector<std::auto_ptr<T>> 示例

Java:String 对象的线程安全

linux - 我应该为 Linux 上最长的用户名或组名创建多大的缓冲区?

c++ - 将 iostream 与 stdio 同步

c++ - std::accumulate 的迭代器

string - SQL 比较字符串

java - 如何将反斜杠后跟 't' 或文件路径中转义字符中的任何其他字母或字符替换为 java 中的正斜杠?

python - 如何使用 python 在内存中查找 00000000 就像 C 使用 while != 00000000 的方式

Java ArrayList 字符串长度打印