C++ 中的 vector<int>::size_type
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4849632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
vector<int>::size_type in C++
提问by Simplicity
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 functionof class X.
例如,对于X::x(){...},我们说它x()是 a member functionof class X。
回答by fredoverflow
size_typeis a (static) member typeof the type vector<int>. Usually, it is a typedeffor std::size_t, which itself is usually a typedeffor unsigned intor unsigned long long.
size_type是type的(静态)成员类型vector<int>。通常,它是一个typedeffor std::size_t,它本身通常是一个typedefforunsigned int或unsigned long long。
回答by unwind
I would read it as "declare x as a variable of a type suitable for holding the size of a vector". 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 longor size_tetc directly as you'd otherwise need to do.
我将其读作“将 x 声明为适合保存向量大小的类型的变量”。vector 为其长度定义了自己的类型,如果可能的话,使用它总是最干净的,而不是“猜测”并直接使用int, unsigned int, long,unsigned long或size_t等,否则你需要这样做。
回答by T33C
vector is a template
矢量是一个模板
so the vectortype templated with inthas a member typedefcalled size_type. xis defined as a variable of that type.
所以vector模板化的类型int有一个typedef名为size_type. x被定义为该类型的变量。

