[英]Why text.size() returns std::size_t and not std::string::size_type
I am studying c++ and currently reading C++ Primer (5th ed).我正在学习 c++,目前正在阅读 C++ Primer(第 5 版)。 I am on the topic about
std::string
.我正在讨论关于
std::string
的话题。 The size
member returns the length of the string. size
成员返回字符串的长度。 The book says (p. 88)这本书说(第88页)
auto len = line.size(); // len has type string::size_type
But Visual Studio Code identifies len
as having the type std::size_t
.但是 Visual Studio Code 将
len
标识为std::size_t
类型。 Why is it different?为什么不一样?
identifies len as having the type
std::size_t
.将 len 标识为
std::size_t
类型。 Why is it different?为什么不一样?
From microsoft's basic_string documentation :来自微软的 basic_string 文档:
typedef typename allocator_type::size_type size_type;
Remarks
评论
it's equivalent to
allocator_type::size_type
.它相当于
allocator_type::size_type
。For type
string
, it's equivalent tosize_t
.对于
string
类型,它相当于size_t
。
(emphasis mine) (强调我的)
Thus, as can be seen from the above quoted remark, for std::string
, it is equivalent to size_t
.因此,从上面引用的注释可以看出,对于
std::string
,它等价于size_t
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.