检查 stl 容器中元素的类型 - c++
我怎样才能获得STL容器所保存的元素的类型?
how can i get the type of the elements that are held by a STL container?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我怎样才能获得STL容器所保存的元素的类型?
how can i get the type of the elements that are held by a STL container?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
对于一般容器来说,它将是
X::value_type
。对于关联容器,它将是X::mapped_type
(X::value_type
对应于pair
)。它是根据C++标准第23章。要检查类型是否相等,您可以使用
boost::is_same
。从 C++11 开始 -std::is_same
。For containers in general it will be
X::value_type
. For associative containers it will beX::mapped_type
(X::value_type
corresponds topair<const Key,T>
). It is according to Chapter 23 of C++ Standard.To check that types are equal you could use
boost::is_same
. And since C++11 —std::is_same
.检查两种类型是否相同可以这样实现(没有 RTTI,值在编译时可用):(
这基本上就是
boost::is_same
的工作原理,减去了某些编译器的解决方法。)Checking whether two types are the same can be achieved like this (without RTTI, value is usable at compile-time):
(This is basically how
boost::is_same
works, minus workarounds for certain compilers.)用户定义的容器类型 - 称之为
ContainerType
- 支持ContainerType::begin ()
和ContainerType::end ()
方法,可以使用,在 C++ 20 中,用于在需要时引用数据类型。这是一个例子
A user defined container type - call it
ContainerType
- that supportsContainerType::begin ()
, andContainerType::end ()
methods could use, in C++ 20,to reference the type of data if needed. Here's an example
从什么意义上说?
也许使用 RTTI 和 typeid()?
你必须使用container::valuetype,其中container是你的容器的名称(例如std::vector)
也许
In what sense?
Maybe using RTTI and typeid()?
Probably you have to use container::valuetype where container is the name of your container (for example std::vector)
Alek
使用这样的东西:
Alek
Use something like this:
Alek
您需要为我们提供更多背景信息。如果您的意思是希望在编译时知道该值,以便轻松更改它,那么请使用
container::value_type
。如果您的意思是容器可能包含各种具体(派生)类型并且您希望在运行时了解它们,那么您可能应该重新评估您的方法。在面向对象编程中,在运行时隐藏类型有时是一种强大的方法,因为这意味着您对正在使用的内容做出更少的假设。您当然可以使用 RTTI,但可能有更好的方法:我们需要更多上下文来说明。
如果您想比较类型,那么您可能会前往运行时路径。 C++ 支持多态性,这本质上是您所关注的类型比较——但内置于该语言中。您想根据类型执行一组不同的指令吗?多态性允许您根据对象的类型执行不同的函数。您无需编写任何额外的代码——只需从公共基础派生即可。
You need to give us more context. If you mean you want the value known at compiletime so it's easy to change it then use
container::value_type
.If what you mean is that the container may hold various concrete (derived) types and you wish to know them at runtime then you should probably re-evaluate your approach. In object-oriented programming hiding the type at runtime is sometimes a powerful approach, because it means you make fewer assumptions about what you're working with. You can of course use RTTI, but there's probably a better way: we'd need more context to tell.
If you wish to compare types then you're probably heading the runtime path. C++ supports polymorphism, which is essentially that type-comparison you're looking after -- but built into the language. You want to execute a different set of instructions based on the type? Polymorphism allows you to execute a different function based on the type of the object. You need not write a single extra line of code -- only derive from a common base.
鉴于类型是静态已知的,您可以通过使用模板专门化来检查它们是否静态相同,而无需使用 rtti。例如使用类似 http: //www.boost.org/doc/libs/1_40_0/libs/type_traits/doc/html/boost_typetraits/reference/is_same.html 或者如果 boost 不可用,请自行推出
given the types are known statically you can check they are the same statically without using rtti by using template specialization. e.g. use something like http://www.boost.org/doc/libs/1_40_0/libs/type_traits/doc/html/boost_typetraits/reference/is_same.html or if boost isn't available roll your own