Think of sizeof
being compile-time evaluable. It evaluates to the size of the type, not the size of the contents. You can even write sizeof(std::string)
which will be exactly the same as sizeof(foo)
for any std::string
instance foo
.
To compute the number of characters in a std::string
, use size()
.
If you have a character array, say char c[6]
then the type of c
is an array of 6 char
s. So sizeof(c)
(known at compile-time) will be 6 as the C++ standard defines the size of a single char
to be 1.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…