HOWTO · C++
The value_type in STL Containers in C++
Learn what an STL container's value_type names, how to access it through a container type, and how to use it in generic C++ code.
On this page
An STL container’s value_type is a type alias for the type of element it stores. For std::vector<int>, that type is int; for an associative container such as std::map<std::string, int>, it is a key-value pair. You can name this alias through the container type, then use it in generic code when the container type is a template parameter.
What value_type Means
Containers define value_type to identify the type of each element in their sequence or collection. For example, std::vector<int>::value_type is int, while a map’s element type is a pair containing its key and mapped value. For std::map<std::string, int>, that element is a std::pair whose first member is a key and whose second member is the associated value; the key is const in the stored pair so ordering remains valid. The alias gives code that works with a container a way to refer to its element type without spelling that type separately. It describes one stored element, not the container object itself: a vector of integers has elements of type int, while the vector has a distinct container type.
Access a Container’s value_type
Use the container’s type before the scope operator: std::vector<int>::value_type. This is a type name, so myVector::value_type is not valid when myVector is an object. If you want a local name for the element type, write using Element = std::vector<int>::value_type;. The spelling Container::value_type is also used when Container is a template parameter; because it is a dependent type name, C++ requires typename before it. The compiler cannot know from the template declaration alone whether a dependent name refers to a type or a value, so typename makes that interpretation explicit. In ordinary code where the container type is already concrete, as in std::vector<int>::value_type, the compiler can see the nested type directly and no such disambiguation is needed.
Use value_type in Generic Code
A generic function can use typename Container::value_type to name the element type while accepting different containers. This example prints each element from a vector and a list:
Run this example:
#include <iostream>
#include <list>
#include <string>
#include <vector>
template <typename Container>
void printElements(const Container& container) {
for (const typename Container::value_type& element : container) {
std::cout << element << '\n';
}
}
int main() {
const std::vector<int> numbers{1, 2, 3};
const std::list<std::string> colors{"red", "blue"};
printElements(numbers);
printElements(colors);
}
The observed output is:
1
2
3
red
blue
In the function, Container is deduced from the argument, so the same body works for both std::vector<int> and std::list<std::string>. Container::value_type depends on that template parameter, which is why the declaration uses typename. The loop binds each element through a const reference: it can read the element without copying it, and it cannot modify the container’s element through that reference. This pattern is useful when a function needs to name an element type for a local variable, parameter, or return type while staying independent of one specific container. The container still needs to support the operations the function uses; here, range-based iteration is the shared requirement.
What value_type Does Not Do
value_type names an element type; it does not allocate or free memory. A container and its allocator handle storage according to their own rules. The alias also does not make one container universally faster or safer than another. Its practical benefit here is that generic code can name the element type supplied by each container, including user-defined element types. It does not promise that every container with a value_type has the same interface: code must still rely only on operations supported by the containers it accepts. Choose a container based on the needed operations and performance characteristics, then use its alias when the element type should follow that choice. In short, value_type tells generic code what an element is; it does not dictate how the container stores or manages those elements.