I try to reuse an STL iterator, but cannot find any info about this. Got problem in this code:
std::vector< boost::shared_ptr<Connection> >::iterator poolbegin = pool.begin();
std::vector< boost::shared_ptr<Connection> >::iterator poolend = pool.end();
if( order ) {
poolbegin = pool.rbegin(); // Here compilation fails
poolend = pool.rend();
}
for( std::vector< boost::shared_ptr<Connection> >::iterator it = poolbegin; it<poolend; it++) {
But getting error:
error: no match for ‘operator=’ in ‘poolbegin = std::vector<_Tp,
_Alloc>::rbegin() with _Tp = boost::shared_ptr, _Alloc = std::allocator >’
Is there a way to reset iterator to new value? Like shared_ptr::reset ?
It looks like you want to have a loop that goes forward or backward through a vector, depending on some condition.
One way to do this is to factor out the loop body into a functor (or lambda if you have C++11).
Now you can have two alternatives for which way you want to go through the loop: