SHARE
TWEET

Finding a list middle

a guest Aug 17th, 2010 362 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. typedef std::list::const_iterator iterator;
  2. typedef pair<iterator*, unsigned> mlpair;
  3.  
  4. //Returns an iterator pointing to an element before the middle of the list
  5. inline
  6. mlpair middle_length(
  7.   iterator begin,
  8.   const iterator & end
  9.   unsigned pos=0,
  10. ) {
  11.   if (begin == end)
  12.     return make_pair(begin, pos); // Total length found
  13.   mlpair n = middle_length(begin+1, end, pos+1);
  14.   if (pos >= int(n.second)/2 - 1 ) // True for all positions greater or equal needed
  15.     n.first=begin;
  16.   return n; // Before target position just return the one already found
  17. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top