Patrick Oscity
Patrick Oscity

Reputation: 54734

check type of element in stl container - c++

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

Upvotes: 18

Views: 12655

Answers (8)

Catriel
Catriel

Reputation: 667

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;
}

Upvotes: 1

Kirill V. Lyadvinsky
Kirill V. Lyadvinsky

Reputation: 99685

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.

Upvotes: 20

KeatsPeeks
KeatsPeeks

Reputation: 19337

container::value_type

Upvotes: 22

UncleBens
UncleBens

Reputation: 41351

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

Upvotes: 5

jk.
jk.

Reputation: 14004

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

Upvotes: 0

wilhelmtell
wilhelmtell

Reputation: 58715

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.

Upvotes: 0

Aleksander Stankiewicz
Aleksander Stankiewicz

Reputation: 550

Use something like this:

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

Alek

Upvotes: 0

Aleksander Stankiewicz
Aleksander Stankiewicz

Reputation: 550

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

Upvotes: 0

Related Questions