Ravi Shankar
Ravi Shankar

Reputation: 2655

what is meaning of member types in stl container?

My question is based upon the following link:

http://www.cplusplus.com/reference/vector/vector/

What is the actual meaning of member types(value_type, allocator_type, etc.) ?

I have searched for this in many text books, but no one defines and explains in a clear way.

Please, can anyone explain it with the help of an example or a clear link? Thanks in advance.

Upvotes: 1

Views: 666

Answers (1)

Cameron
Cameron

Reputation: 98836

They are typedefs to the corresponding types that the templated container is using.

For example, value_type corresponds to the type of the element that the vector can hold. So std::vector<int>::value_type would be int, and std::vector<float>::value_type would be float.

Having the commonly-used types available as a type on the container is useful when the container's type itself is unknown. For example, someone may want to write library code that works equally well with std::map and std::unordered_map:

template<typename TMap>
void insert_default_pair(TMap& map)
{
    map.emplace(typename TMap::key_type(), typename TMap::mapped_type());
}

Upvotes: 1

Related Questions