简体   繁体   English

检查stl容器中的元素类型 - c ++

[英]check type of element in stl container - c++

我怎样才能获得STL容器所持有的元素类型?

container::value_type

For containers in general it will be X::value_type . 对于容器,一般来说它是X::value_type For associative containers it will be X::mapped_type ( X::value_type corresponds to pair<const Key,T> ). 对于关联容器,它将是X::mapped_typeX::value_type对应于pair<const Key,T> )。 It is according to Chapter 23 of C++ Standard. 这是根据C ++标准的第23章。

To check that types are equal you could use boost::is_same . 要检查类型是否相等,可以使用boost::is_same

Checking whether two types are the same can be achieved like this (without RTTI, value is usable at compile-time): 检查两种类型是否相同可以像这样实现(没有RTTI,值在编译时可用):

template <class T, class U>
struct same_type
{
    static const bool value = false;
};

//specialization for types that are the same
template <class T>
struct same_type<T, T>
{
    static const bool value = true;
};

//sample usage:
template <class FirstContainer, class SecondContainer>
bool containers_of_same_type(const FirstContainer&, const SecondContainer&)
{
    return same_type<
        typename FirstContainer::value_type, 
        typename SecondContainer::value_type
    >::value;
}

#include <vector>
#include <list>
#include <iostream>

int main()
{
    std::cout << containers_of_same_type(std::vector<int>(), std::list<int>());
    std::cout << containers_of_same_type(std::vector<char>(), std::list<int>());
}

(This is basically how boost::is_same works, minus workarounds for certain compilers.) (这基本上是boost::is_same工作方式,减去某些编译器的变通方法。)

In what sense? 凭什么? Maybe using RTTI and typeid()? 也许使用RTTI和typeid()?

Probably you have to use container::valuetype where container is the name of your container (for example std::vector) 可能你必须使用container :: valuetype,其中container是容器的名称(例如std :: vector)

Alek 阿列克

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 . 如果你的意思是你想在编译时知道值,那么很容易改变它,然后使用container::value_type

typedef vector<int> coordinates;

coordinates seq;
fib::value_type elem = seq.back(); // it's easy to change int 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. 你当然可以使用RTTI,但可能有更好的方法:我们需要更多的上下文来讲述。

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. C ++支持多态,这实际上是你正在寻找的类型比较 - 但是内置于语言中。 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. 您不需要编写一行额外的代码 - 只能从一个公共基础派生。

Use something like this: 使用这样的东西:

if (typeid(yourVariable)==typeid(YourClass)) //...

Alek 阿列克

given the types are known statically you can check they are the same statically without using rtti by using template specialization. 鉴于这些类型是静态已知的,您可以通过使用模板特化来检查它们是否静态相同而不使用rtti。 eg 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 例如,使用类似http://www.boost.org/doc/libs/1_40_0/libs/type_traits/doc/html/boost_typetraits/reference/is_same.html的内容,或者如果无法使用提升,请自行滚动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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