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

发布于 2024-08-11 05:40:04 字数 29 浏览 4 评论 0原文

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

how can i get the type of the elements that are held by a STL container?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

所谓喜欢 2024-08-18 05:40:04
container::value_type
container::value_type
妖妓 2024-08-18 05:40:04

对于一般容器来说,它将是X::value_type。对于关联容器,它将是 X::mapped_typeX::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 be X::mapped_type (X::value_type corresponds to pair<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.

山川志 2024-08-18 05:40:04

检查两种类型是否相同可以这样实现(没有 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>());
}

这基本上就是 boost::is_same 的工作原理,减去了某些编译器的解决方法。)

Checking whether two types are the same can be achieved like this (without RTTI, value is usable at compile-time):

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.)

手心的温暖 2024-08-18 05:40:04

用户定义的容器类型 - 称之为 ContainerType - 支持 ContainerType::begin ()ContainerType::end () 方法,可以使用,在 C++ 20 中,

using DataType = std::ranges::range_value_t<ContainerType>;

用于在需要时引用数据类型。这是一个例子

#include <iostream>
#include <array>
#include <vector>
#include <ranges>

template <typename T, size_t N>
struct MyContainer
{   
    T * begin () { return m_Arr.data (); }
   
    T * end () { return m_Arr.data () + m_Arr.size (); }
   
    std::array<T, N> m_Arr;
};

template <typename ContainerType> std::vector<std::ranges::range_value_t<ContainerType>> ToVector (ContainerType cc)
{
    using DataType = std::ranges::range_value_t<ContainerType>;
    std::vector<DataType> res;
    for (const auto &value: cc)
    {
        res.push_back (value);
    }
    
    return res;
}


int main ()
{
    MyContainer<double, 4> cc{{0.5, 1., 2., 3.}};
    const auto myVector = ToVector (cc);
    
    for (auto vv: myVector)
        std::cout << vv << "\n";
    
    return 0;
}

A user defined container type - call it ContainerType - that supports ContainerType::begin (), and ContainerType::end () methods could use, in C++ 20,

using DataType = std::ranges::range_value_t<ContainerType>;

to reference the type of data if needed. Here's an example

#include <iostream>
#include <array>
#include <vector>
#include <ranges>

template <typename T, size_t N>
struct MyContainer
{   
    T * begin () { return m_Arr.data (); }
   
    T * end () { return m_Arr.data () + m_Arr.size (); }
   
    std::array<T, N> m_Arr;
};

template <typename ContainerType> std::vector<std::ranges::range_value_t<ContainerType>> ToVector (ContainerType cc)
{
    using DataType = std::ranges::range_value_t<ContainerType>;
    std::vector<DataType> res;
    for (const auto &value: cc)
    {
        res.push_back (value);
    }
    
    return res;
}


int main ()
{
    MyContainer<double, 4> cc{{0.5, 1., 2., 3.}};
    const auto myVector = ToVector (cc);
    
    for (auto vv: myVector)
        std::cout << vv << "\n";
    
    return 0;
}
土豪 2024-08-18 05:40:04

从什么意义上说?
也许使用 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

ゝ偶尔ゞ 2024-08-18 05:40:04

使用这样的东西:

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

Alek

Use something like this:

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

Alek

执手闯天涯 2024-08-18 05:40:04

您需要为我们提供更多背景信息。如果您的意思是希望在编译时知道该值,以便轻松更改它,那么请使用 container::value_type

typedef vector<int> coordinates;

coordinates seq;
fib::value_type elem = seq.back(); // it's easy to change int 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.

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.

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.

强者自强 2024-08-18 05:40:04

鉴于类型是静态已知的,您可以通过使用模板专门化来检查它们是否静态相同,而无需使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文