vector<int>::size_type in C++

旧巷老猫 提交于 2019-11-26 06:34:32

问题


What is meant by this C++ statement?

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.


回答1:


size_type is a (static) member type of the 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.




回答2:


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 long or size_t etc directly as you'd otherwise need to do.




回答3:


vector is a template

so the vector type templated with int has a member typedef called size_type. x is defined as a variable of that type.



来源:https://stackoverflow.com/questions/4849632/vectorintsize-type-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!