Arrays
You can find the size of an array with the following template function:
template <typename T, size_t N> size_t size(T (&)[N]) { return N; }
Example:
#include <iostream> int main() { int ints[] = {1, 2, 3, 4}; const char *pchars[] = {"a", "b", "c", "d"}; std::cout << size(ints) << "\n"; std::cout << size(pchars) << "\n"; }
4 4
Note that once an array has decayed to a pointer by being passed to a function, this will not work.
Containers
You can extend this to containers with the following two functions, which take template template parameters:
template <template<class, class> class Container, class T, class Allocator> size_t size(const Container<T, Allocator>& cont) { return cont.size(); } template <template<class, class, class, class> class Container, class Key, class T, class Compare, class Allocator> size_t size(const Container<Key, T, Compare, Allocator>& cont) { return cont.size(); }
Example:
#include <iostream> #include <vector> #include <algorithm> #include <map> int main() { std::vector<int> vec(10); std::iota(vec.begin(), vec.end(), 0); std::map<std::string, int> dict; dict["apples"] = 7; dict["pears"] = 9; std::cout << size(vec) << "\n"; std::cout << size(dict) << "\n"; }
10 2
C++11
In C++11, you can use decltype
to select the container overload, so the two template template functions can be replaced with this:
template <class Container> auto size(const Container& cont) -> decltype(cont.size()) { return cont.size(); }
C++17
C++17 has added the std::size()
function, which probably uses the size_t
overload for arrays, and the one using decltype
for containers.