2012-01-05 7 views

答えて

5
// Filters the elements of a set: elements that satisfy the predicate 'pred' 
// are removed from the source set and inserted into the output. 
template <typename TSet, typename TOutputIterator, typename TPredicate> 
void extract_if(TSet& s, TOutputIterator out, TPredicate pred) 
{ 
    for (typename TSet::iterator it(s.begin()); it != s.end();) 
    { 
     if (pred(*it)) 
     { 
      *out++ = *it; 
      it = s.erase(it); 
     } 
     else 
     { 
      ++it; 
     } 
    } 
} 
+0

うわー、気付かれなかった 'erase'はイテレータを返します:) –

+1

私はそれを試してみたのですが、あなたのループは' if'文の 'else'節で' it'をインクリメントするべきではありません。 for文よりも? – Hurkyl

+0

@Hurkyl:確かにそれはすべきです。おっとっと。ありがとうございました! –

関連する問題