// Demonstrating use of count and find algorithms with list #include #include #include int main() { cout << "Demonstrating use of simple STL count & find algorithms:" << endl; list list2; // populating the list using "push_back" list2.push_back(89); list2.push_back(99); list2.push_back(89); list2.push_back(69); list2.push_back(59); list2.push_back(89); list2.push_back(89); list2.push_back(49); // retrieve all values in list2 list::iterator vp; cout << "Values in List2: [ " ; for (vp = list2.begin(); vp != list2.end(); vp++) cout << *vp << " " ; cout << " ]" << endl; // count algorithm... cout << "89 occurs: " << count(list2.begin(), list2.end(), 89) << endl; cout << "63 occurs: " << count(list2.begin(), list2.end(), 63) << endl; cout << "59 occurs: " << count(list2.begin(), list2.end(), 59) << endl; // find algorithm vp = find(list2.begin(), list2.end(), 59); if (vp != list2.end()) // test to see if the item 59 was found cout << "59 was found in list2" << endl; else cout << "59 was not found in list2" << endl; // illustrating "++" and "--" operators on "iterators" list::iterator np = vp; list::iterator pp = vp; if ( np != list2.end()) np++; if (np != list2.end()) cout << "Item after first 59 is: " << *np << endl; else cout << "No item after 59" << endl; if (pp != list2.end()) pp--; if (pp != list2.end()) cout << "Item before first 59 is: " << *pp << endl; else cout << "No item before 59" << endl; vp = find(list2.begin(), list2.end(), 63); if (vp != list2.end()) // test to see if the item 63 was found cout << "63 was found in list2" << endl; else cout << "63 was not found in list2" << endl; }