gpt4 book ai didi

c++ - std::size_t vs size_t vs std::string::size_type

转载 作者:IT老高 更新时间:2023-10-28 21:57:58 31 4
gpt4 key购买 nike

  • size_t 在哪里什么时候我什么都没有?
  • 总是假设 size_t 是否合理? == std::size_t ?
  • 什么时候应该使用 size_typestd容器(string::size_typevector<T>::size_type 等)?

最佳答案

Where does size_t come from when I don't have anything included in an empty project?

如果您没有包含任何内容,则无法使用 size_t .它在 <stddef.h> 中定义(也可能在 <cstddef> 中,如果您的该 header 版本将定义放在全局命名空间以及 std 中)。

Is it reasonable to always assume size_t == std::size_t?

是的。 C 库定义的所有类型和函数都包含在 std 中。命名空间,只要您包含适当的 C++ header (例如 <cstddef> 而不是 <stddef.h> )

When should I use std::_::size_type?

你的意思是size_type在一些标准类和模板中定义的类型,例如 vector ?如果您愿意,可以在使用这些类时使用它们。在大多数情况下,您会知道它与 size_t 相同。 ,因此您不妨使用它并节省一些打字时间。如果您正在编写通用代码,而您不知道类是什么,那么最好使用 size_type如果它与 size_t 不兼容.

例如,您可能想要编写一个容器,用于容纳比 size_t 所能表示的更多的项目。 .您可以使用某种大数字类型来表示容器的大小,它不能转换为 size_t .在这种情况下,像 size_t s = c.size() 这样的代码将无法编译 - 您需要使用 Container::size_type而是。

关于c++ - std::size_t vs size_t vs std::string::size_type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8507851/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com