1: //list08.cpp 3: #include <iostream> 4: #include <list> 5: using namespace std; 7: int numberOfOddValues 8: ( 9: const list<int>& lst //in 10: ); 11: /**< 12: Count the odd integers in a list of integers. 13: @return The number of odd integer values in the input list. 14: @param lst A list of integers. 15: @pre lst has been initialized. 16: @post No side effects. 17: */ 19: int main() 20: { 21: cout << "\nThis program illustrates a typical use of a const " 22: "iterator of the list class."; 23: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 25: cout << "\nHere are the contents of a list of size 12:\n"; 26: int a[] = {1, 4, 6, 8, 11, 13, 15, 16, 19, 20, 23, 25}; 27: list<int> lst(a, a+12); 28: list<int>::iterator p = lst.begin(); 29: while (p != lst.end()) cout << *p++ << " "; 30: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 32: cout << "\nThe number of odd values in the list is " 33: << numberOfOddValues(lst) << ".\n"; 34: cout << "Press Enter to continue ... "; cin.ignore(80, '\n'); 35: } 38: int numberOfOddValues 39: ( 40: const list<int>& lst //in 41: ) 42: { 43: int oddCount = 0; 44: //Since lst is a "const" input parameter, you must use 45: //a const_iterator inside the function to access lst. 46: list<int>::const_iterator p = lst.begin(); 47: while (p != lst.end()) 48: { 49: if (*p % 2 == 1) ++oddCount; 50: ++p; 51: } 52: return oddCount; 53: }