简体   繁体   English

为什么 text.size() 返回 std::size_t 而不是 std::string::size_type

[英]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 to size_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.

相关问题 std :: size_t vs size_t vs std :: string :: size_type - std::size_t vs size_t vs std::string::size_type 为什么 std::array size_t 和 std::vector 中的 size_type 通常是 size_t? - Why is size_type in std::array size_t and in std::vector usually size_t? std :: size_t或std :: vector <Foo> ::尺码​​类型? - std::size_t or std::vector<Foo>::size_type? 保证std :: container :: size_type是std :: size_t - Guarantee that std::container::size_type is a std::size_t 使用std :: string :: size_type比size_t有什么好处吗? - Is there any benefit to using std::string::size_type over size_t? 对于具有默认分配器的标准容器,std :: container :: size_type是否保证为size_t? - Is std::container::size_type guaranteed to be size_t for standard containers with default allocator? size_type可以大于std :: size_t吗? - Can a size_type ever be larger than std::size_t? std::size_t 与 size_type 作为参数和 function 返回类型 - std::size_t vs. size_type as parameters and function return types 关于类型`std::basic_string的问题<char> ::size_type` 和 `size_t` 在如下所示的解决方案中</char> - Question about the types `std::basic_string<char>::size_type` and `size_t` in the solution shown below 向量索引变量声明(size_t 或 std::vector<DATATYPE> ::尺码​​类型) - Vector index variable declaration (size_t or std::vector<DATATYPE>::size_type)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM