You probably donβt want to know the size of the vector in bytes, because the vector is a non-trivial object that is separated from the contents located in dynamic memory.
std::vector<int> v { 1, 2, 3 };
What you probably want to know is the size of the data, the number of bytes needed to store the current contents of the vector. For this you can use
template<typename T> size_t vectorsizeof(const typename std::vector<T>& vec) { return sizeof(T) * vec.size(); }
or you can just do
size_t bytes = sizeof(vec[0]) * vec.size();
source share