如何声明vector :: size_type的向量? [英] How do I declare a vector of vector::size_type?

查看:57
本文介绍了如何声明vector :: size_type的向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个向量,其元素的类型为vector :: size_type

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

但是,您不能声明:

vector<vector::size_type> aVec;

因为size_type是模板本身的一部分,所以您必须使用类型本身,我需要类似的东西:

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;

但是T应该是什么?这确实是一个循环问题.:)

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

如果vector刚好使用size_t作为其大小类型(但对size_type没有特殊的typedef,它可能会根据vector所持有的类型而有所不同),我可以这样做:

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.

有想法吗?

推荐答案

(我在这里重新写一个答案,这次没有对标准做任何声明.)

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

您可以针对类型(最好是 std :: size_t std :: vector< void *> :: size_type)做出最佳猜测),然后检查事实.

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");

这篇关于如何声明vector :: size_type的向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆