//sample code to show declaration of iterators // and also use of find function (in STL Algorithms) #include #include #include using namespace std; // PrintCollection - from list class notes template void PrintCollection( const Container & c, ostream & out ) { if( c.empty( ) ) out << "(empty)"; else { // note the required keyword typename Container::const_iterator itr = c.begin( ); out << "[ " << *itr++; // Print first item while( itr != c.end( ) ) out << ", " << *itr++; out << " ]" << endl; } } template void Subtract (list & L, const list & E) { list::const_iterator eItr = E.begin(); //E is const, so eItr must be const_iterator. for (;eItr != E.end(); ++eItr){ //Algorithm version of find differs from class version. list:: iterator found= find(L.begin(), L.end(), *eItr); while (found !=L.end()) { L.erase(found); found= find(L.begin(), L.end(), *eItr); } //end while } //end for } //end function int main() { list L1; list L2; //put some stuff in the 2 lists for (int z= 1; z < 20; z+= 2) L1.push_back(z); cout << "List contents before subtraction" << endl; PrintCollection (L1, cout); for(int x=2; x < 14; x+=3) L2.push_back(x); cout << "Removing all occurrences from this list:" << endl; PrintCollection (L2, cout); Subtract(L1, L2); cout << "after subtraction" << endl; PrintCollection (L1, cout); }