[英]What type of value should it be returned by getters that return size of vector?
What type of value should be returned by getters that return size of vector? 返回向量大小的getter应该返回什么类型的值?
For example I have many getters in my project of the following type and I need the (int)
cast to remove the compile warnings: 例如,我在以下类型的项目中有许多getter,我需要(int)
强制转换来删除编译警告:
int getNumberOfBuildings() const { return (int)buildings.size(); }
The C++03 way: C ++ 03方式:
std::vector<Building>::size_type getNumberOfBuildings() const
{ return buildings.size(); }
The C++11 way: C ++ 11方式:
auto getNumberOfBuildings() const -> decltype(buildings.size())
{ return buildings.size(); }
The C++14 way: C ++ 14方式:
auto getNumberOfBuildings() const
{ return buildings.size(); }
You should return a std::size_t
. 你应该返回一个std::size_t
。 std::size_t
is the return type of vector<T>::size()
when you use the default allocator. 当您使用默认分配器时, std::size_t
是vector<T>::size()
的返回类型。 vector<T>::size()
returns vector<T>::size_type
which is just a typedef for std::size_t
when you use the default allocator (which you are most likely using). vector<T>::size()
返回vector<T>::size_type
,当你使用默认分配器(你最有可能使用它)时,它只是std::size_t
的typedef。
std::vector
provides a typedef, std::vector<>::size_type
, that indicates which is the type used as the result of vector::size
: std::vector
提供了一个typedef, std::vector<>::size_type
,它指示哪个是用作vector::size
结果的类型:
std::vector<TYPE>::size_type getNumberOfBuildings() const { return buildings.size(); }
This, however, is usually size_t
, so you can go ahead and use the latter directly: 但是,这通常是size_t
,因此您可以直接使用后者:
#include <cstddef>
std::size_t getNumberOfBuildings() const { return buildings.size(); }
The formal answer is indeed std::vector<Building>::size_type
, as stated in other answers. 正式答案确实是std::vector<Building>::size_type
,如其他答案中所述。
The real answer depends on whether you already have a type in your program that you use to represent the number of buildings. 真正的答案取决于您是否已在程序中使用了用于表示建筑物数量的类型。 It could be int
, it could be unsigned
or it could be some typedef name TBuildingCount
. 它可以是int
,可以是unsigned
,也可以是某个typedef名称TBuildingCount
。 This is the type you should use in this case. 这是您在这种情况下应该使用的类型。 It might require a cast to suppress compiler warnings, but that's the way it usually is. 它可能需要强制转换来抑制编译器警告,但这通常是这样。
I don't see the rest of your code, so I can't say it for sure, but I'd guess that the fact that the buildings are stored in some vector somewhere (or in any other container) is just an implementation detail, which should not influence your choice of type for counting these buildings. 我没有看到你的其余代码,所以我不能肯定地说,但我猜想建筑物存放在某个地方(或任何其他容器)的某个矢量这一事实只是一个实现细节,这不应影响您对这些建筑物进行计数的类型选择。 There's no need to expose the existence of that containber (or rely on it) even in such indirect way as using its size_type
to return the count. 即使以使用其size_type
返回计数的间接方式,也不需要暴露该包含者的存在(或依赖它)。
In other words, if you already decided to use int
for counting buildings, then the implementation in your question is the one you should stick with. 换句话说,如果您已经决定使用int
来计算建筑物,那么您的问题中的实施是您应该坚持的。
In contrast to many other answers, the return value of your function actually much more depends on your interface than what you use under the hood. 与许多其他答案相比,函数的返回值实际上更多地取决于您的界面而不是您在引擎盖下使用的界面。 Are you returning the size of a vector and want everybody to know it? 你是否返回了矢量的大小并希望每个人都知道它? Well, then go ahead and use std::vector::size_type
. 那么,继续使用std::vector::size_type
。 But are you actually returning some abstract counting variable, like the number of buildings, then I don't think a std::vector::size_type
belongs into your interface, since that is just an implementation detail. 但是你实际上是否返回了一些抽象的计数变量,比如建筑物的数量,那么我认为std::vector::size_type
属于你的界面,因为那只是一个实现细节。
If it's a size of something that you return (but not neccessarily known to be a vector under the hood), I'd use good old std::size_t
, but if it's a count of something, then an unsigned int
is conceptually much more appropriate. 如果它是你返回的东西的大小(但不一定知道它是引擎盖下的向量),我会使用好的旧std::size_t
,但如果它是某个东西的数量,那么unsigned int
在概念上更多适当。 Of course nobody guarantees that an unsigned int
or std::size_t
can hold the size of an arbitrary std::vector
, but then again it's not an arbitrary std::vector
, it's an array of buildings and you yourself should know if there can ever be more than std::numeric_limits<unsigned int>::max()
buildings anyway. 当然没有人保证unsigned int
或std::size_t
可以保存任意std::vector
的大小,但是再次它不是一个任意的std::vector
,它是一个建筑物阵列,你自己应该知道是否可以无论如何,永远不会超过std::numeric_limits<unsigned int>::max()
建筑物。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.