boost::distance

References

Headers

boost::distance is available by including any of the following headers:

  • boost/range/distance.hpp or
  • boost/range/algorithm_ext.hpp or
  • boost/range/atl.hpp or
  • boost/range/mfc.hpp or
  • boost/range/numeric.hpp or
  • boost/range/functions.hpp or
  • boost/range/iterator_range_core.hpp or
  • boost/range/iterator_range_io.hpp or
  • boost/range/counting_range.hpp or
  • boost/range/any_range.hpp or
  • boost/range.hpp

Examples

size.cpp

#include <iostream>
#include <string>
#include <vector>

#include <boost/range.hpp>


int main() {
    std::string str = "abcde";
    std::vector<int> vec = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    // boost::empty() and boost::size() do about what you'd expect.
    std::cout << "empty(str): " << boost::empty(str) << std::endl;
    std::cout << "size(vec): " << boost::size(vec) << std::endl;

    // Prefer boost::size() over boost::distance(). The former calls the
    // .size() member function if available and degrades to boost::distance()
    // otherwise. Thus, there's no reason to use boost::distance() directly.
    std::cout << "distance(vec): " << boost::distance(vec) << std::endl;

    return 0;
}

Output:

empty(str): 0
size(vec): 10
distance(vec): 10

 

Boost Range for Humans

This reference is part of Boost Range for Humans. Click the link to the overview.