简体   繁体   中英

How do I declare a vector of vector::size_type?

I want a vector whose elements are of the type vector::size_type

However, you cannot declare:

vector<vector::size_type> aVec;

because size_type is part of the template itself, so you have to use the type itself, I need something like:

vector<vector<T>::size_type> aVec;

but what should the T be? It's really a circular problem. :)

If vector had just used size_t as its size type (but not had a special typedef for the size_type which could vary maybe based on the type that the vector is holding), I could just do:

vector<size_t> aVec;

but, that isn't the case. I suspect there is a valid reason for it being allowed to vary, but it is making this hard by having it part of the templated vector class instead of outside of it.

Thoughts?

std::vector<>::size_typestd::size_t类型的静态成员类型,所以你应该使用std::vector<std::size_t> vec是安全的

Why don't you use vector<vector<int>::size_type> myVector; ? If you never use the type vector<int> anywhere else, it doesn't cost anything as template instanciation is somehow lazy. You can use any other type than int if you which.

It is very unlikely that vector<T>::size_type will be different than vector<T'>::size_type .

(I am rewritting an answer here, without making any claims about the standard this time.)

You can make your best guess for the type (which can be std::size_t or std::vector<void*>::size_type ) and check after the fact.

std::vector<std::size_t> Avec;
static_assert(std::is_same<decltype(Avec)::size_type, decltype(Avec)::value_type>(), "bad guess");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM