[英]vector<int>::size_type in C++
What is meant by this C++ statement?这个 C++ 语句是什么意思?
vector<int>::size_type x;
And, what is the use of the scope operator ::
here?而且,这里的作用域运算符
::
什么用? In other words, how do we read this statement in English?换句话说,我们如何用英语阅读这个声明?
For example, for X::x(){...}
, we say that x()
is a member function
of class X
.例如,对于
X::x(){...}
,我们说x()
是class X
的member function
。
size_type
is a (static) member type of the type vector<int>
. size_type
是vector<int>
类型的(静态)成员类型。 Usually, it is a typedef
for std::size_t
, which itself is usually a typedef
for unsigned int
or unsigned long long
.通常,它是
std::size_t
的typedef
,它本身通常是unsigned int
或unsigned long long
的typedef
。
I would read it as "declare x as a variable of a type suitable for holding the size of a vector".我将其读作“将 x 声明为适合保存向量大小的类型的变量”。 The vector defines its own type for its length, and it's always cleanest to use that if possible, rather than "guessing" and using
int
, unsigned int
, long
, unsigned long
or size_t
etc directly as you'd otherwise need to do.向量为其长度定义了自己的类型,如果可能的话,使用它总是最干净的,而不是“猜测”并直接使用
int
、 unsigned int
、 long
、 unsigned long
或size_t
等,否则你需要这样做。
vector is a template矢量是一个模板
so the vector
type templated with int
has a member typedef
called size_type
.所以用
int
模板化的vector
类型有一个名为size_type
的成员typedef
。 x
is defined as a variable of that type. x
被定义为该类型的变量。
Different implementations use different types to represent sizes, so we cannot write the appropriate type directly and remain implementation-independent.
不同的实现使用不同的类型来表示大小,因此我们不能直接编写适当的类型并保持与实现无关。 For that reason, it is good programming practice to use the size_type that the library defines to represent container sizes.
因此,使用库定义的 size_type 来表示容器大小是一种很好的编程习惯。
- Accelerated C++ by Andrew Koenig and Barbara E. Moo - Andrew Koenig 和 Barbara E. Moo 的加速 C++
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.