The STL has two associative containers: std::map<K,V> and
std::multimap. There is also std::set<V>
which should be an adaptor of std::map<V,void>
, but as such it isn't an associative container. The multimap is similar to the map, only it allows multiple identical keys within the same container. Both map and multimap hold elements of type std::pair<K,V>
. In other words, std::map<K,V>::value_type == std::pair<K,V>
, but std::map<K,V>::key_type == K
and std::map<K,V>::mapped_type == V
.
As for "Type", I am not entirely sure what you mean. If you mean parameterized classes then C++ calls this "Template Programming", or "Generic Programming". In the above, std::map<K,V>
is parameterized over K and V for the keys' type and the values' type. C++ also supports template functions:
template<typename T>
void f(T o);
will declare a function that takes as a parameter any type at all, including primitive types. C++ doesn't support generic type resolution, such that the type T must of certain hierarchy. For now, all you can do is just assume the passed type is indeed of the correct hierarchy, and the compiler will complain if you try to call a non-declared function over an object of that type.
template<typename T>
void f(T o) {
o.do_it();
}
The above will work as long as T defines the method do_it()
.