为什么 std::string 对象的最大大小是 4611686018427387897?

Вла*_*лад 2 c++ string stl stdstring

我有代码输出对象的最大长度std::string

string s;
cout<<s.max_size()<<endl;
cout << INT_MAX << endl;
cout<<LLONG_MAX<<endl;
Run Code Online (Sandbox Code Playgroud)

输出 :

4611686018427387897
2147483647
9223372036854775807
Run Code Online (Sandbox Code Playgroud)

如果不难的话,请解释一下为什么 的最大尺寸std::string是 4611686018427387897。这大约是 的一半LLONG_MAX

Mar*_*low 5

这取决于basic_string您使用的标准库中的实现方式。不同的实现可能会“使用”字符串大小中的一部分用于其他目的(例如,表示“这是一个短字符串”的标志)。