![]() |
Home | Libraries | People | FAQ | More |
template <typename Container> std::pair< shared_container_iterator<Container>, shared_container_iterator<Container> > make_shared_container_range(boost::shared_ptr<Container> const& container); Class shared_container_iterator is meant primarily to return, using iterators, a range of values that we can guarantee will be alive as long as the iterators are. This is a convenience function to do just that. It is equivalent to std::make_pair(make_shared_container_iterator(container->begin(),container), make_shared_container_iterator(container->end(),container));
In the following example, a range of values is returned as a pair of shared_container_iterator objects.
#include "shared_container_iterator.hpp" #include "boost/shared_ptr.hpp" #include "boost/tuple/tuple.hpp" // for boost::tie #include <algorithm> // for std::copy #include <iostream> #include <vector> typedef boost::shared_container_iterator< std::vector<int> > iterator; std::pair<iterator,iterator> return_range() { boost::shared_ptr< std::vector<int> > range(new std::vector<int>()); range->push_back(0); range->push_back(1); range->push_back(2); range->push_back(3); range->push_back(4); range->push_back(5); return boost::make_shared_container_range(range); } int main() { iterator i,end; boost::tie(i,end) = return_range(); std::copy(i,end,std::ostream_iterator<int>(std::cout,",")); std::cout.put('\n'); return 0; }
Though the range object only lives for the duration of the return_range
call, the reference counted
std::vector
will live until i
and end
are both destroyed. The output from this example is the same as the previous
two.