#include#include #include int main() { std::vector vec{0, 1, 2, 3, 4}; auto first_iter = vec.begin(); auto last_iter = vec.end(); std::cout << "The size of the range is: " << std::distance(first_iter, last_iter) << std::endl; return 0; }
#includeIn this example, we use the std::find algorithm to find the iterator to the element 3 in the vector. Then, we use std::distance to calculate the distance between the beginning of the vector and the iterator to the element 3. Both examples use the C++ Standard Library, so it is a part of the standard library package.#include #include int main() { std::vector vec{0, 1, 2, 3, 4}; auto iter = std::find(vec.begin(), vec.end(), 3); std::cout << "The distance between the beginning and the element 3 is: " << std::distance(vec.begin(), iter) << std::endl; return 0; }