// Oldham, Jeffrey D. // 2000Jan31 // CS 1321 // Print a Sorted List of Names // 1. Read in a list of names from standard input. // 2. Sort the names in a case-insensitive way. // 3. Print the names to standard output. #include #include // has EXIT_SUCCESS #include #include #include #include // has ostream_iterator #include // has tolower() // Helper Functions // Compare two strings in a case-insensitive way. // Returns true only if first parameter < second parameter. bool caseInsensitiveComp(string left, string right) { // Convert the strings to all lowercase. transform(left.begin(), left.end(), left.begin(), tolower); transform(right.begin(), right.end(), right.begin(), tolower); // Compare the strings. return left < right; // string class has < } // Print a string to standard output. void printString(const string & str) { cout << str << endl; } int main() { vector v; // Store the names into a vector. string name; while (cin >> name) v.push_back(name); // Sort the names in a case-insensitive way. sort(v.begin(), v.end(), caseInsensitiveComp); // Print the names to the standard output. // The last argument says to send strings to cout separated by "\n". for_each(v.begin(), v.end(), printString); return EXIT_SUCCESS; } // Here is another one way to read in the strings from cin. // The first parameter says to read strings from cin. // The second parameter indicates the end of reading. // The third iterator calls push_back() on the vector v. // copy(istream_iterator(cin), // istream_iterator(), // back_inserter(v));