_THE C++ STANDARD LIBRARY_ by Michael Vilot Listing One #include // C library functions #include // are in the global namespace int main() { char h[] = "hello, "; char w[] = "world\n"; char hw[sizeof h + sizeof w]; strcpy(hw, h); // catenate strings strcat(hw, " "); strcat(hw, w); printf(hw); // output return 0; } Listing Two #include // same C library functions #include // in namespace std int main() { char h[] = "hello,"; char w[] = "world\n"; char hw[sizeof h + sizeof w]; std::strcpy(hw, h); std::strcat(hw, " "); std::strcat(hw, w); std::printf(hw); return 0; } Listing Three #include #include #include // for EXIT_FAILURE int main(int argc, char* argv[]) // copy file to stdout { FILE* file = fopen(argv[1], "r"); char errbuf[80]; // just in case if (!file) { sprintf(errbuf, "%s no good", argv[1]); perror(errbuf); return EXIT_FAILURE; } while ( !feof(file) ) { char line[256]; // hopefully large enough char* ok = fgets(line, sizeof line, file); if (!ok) { sprintf(errbuf, "read error on %s", argv[1]); perror(errbuf); return EXIT_FAILURE; } printf("[%2d]\t%s", strlen(line), line); // has '\n' already } return 0; } Listing Four [18] End the arms race [ 6] Floss Listing Five #include #include #include // for setw int main(int argc, char* argv[]) // copy file to cout { using namespace std; ifstream in(argv[1]); while (in.good()) { string line; if (getline(in, line)) cout << '[' << setw(2) << line.size()<< ']' << '\t' << line << endl; } return 0; } Listing Six #include #include #include int main(int argc, char* argv[]) { using namespace std; ifstream in(argv[1]); typedef list ToDo_List; ToDo_List to_do; while (in.good()) { string buf; getline(in, buf); to_do.push_back(buf); } typedef ToDo_List::iterator iter; for (iter i = to_do.begin(); i != to_do.end(); ++i) cout << *i << endl; return 0; } Listing Seven #include #include #include // for random access via operator[] #include // for sort() int main(int argc, char* argv[]) { using namespace std; ifstream in(argv[1]); typedef vector ToDo_List; ToDo_List to_do; while (in.good()) { string buf; getline(in, buf); to_do.push_back(buf); } typedef ToDo_List::iterator iter; for (iter i = to_do.begin(); i != to_do.end(); ++i) cout << *i << endl; sort(to_do.begin(), to_do.end()); cout << "\nSorted:" << endl; for (iter j = to_do.begin(); j != to_do.end(); ++j) cout << *j << endl; return 0; } Listing Eight Input: 1. End the arms race 2. Floss Output: 2. Floss 1. End the arms race Listing Nine // todo.h #ifndef TODO_H #define TODO_H #include #include // for istream&, ostream& struct when { int month; // range 1..12 int day; // range 1..31 }; struct to_do { when date; int priority; std::string what; }; int operator<(const to_do& td1, const to_do& td2); int operator>(const to_do& td1, const to_do& td2); std::istream& operator>>(std::istream& in, to_do& td); std::ostream& operator<<(std::ostream& out, const to_do& td); #endif Listing Ten #include #include #include #include // for sort() #include "todo.h" int main(int argc, char* argv[]) { using namespace std; ifstream in(argv[1]); typedef vector ToDo_List; ToDo_List td; while (in.good()) { to_do buf; in >> buf; td.push_back(buf); } typedef ToDo_List::iterator iter; for (iter i = td.begin(); i != td.end(); ++i) cout << *i << endl; sort(td.begin(), td.end()); cout << "\nSorted:" << endl; for (iter j = td.begin(); j != td.end(); ++j) cout << *j << endl; return 0; } Listing Eleven Input: Mar 31 3 Get April Fool's jokes Mar 31 1 Pick up laundry Jun 3 2 Dentist appointment Jan 1 1 Stop the arms race Jun 2 1 Floss Output: Jan 1 1 Stop the arms race Mar 31 1 Pick up laundry Mar 31 3 Get April Fool's jokes Jun 2 1 Floss Jun 3 2 Dentist appointment Listing Twelve #include #include #include #include // for priority_queue #include // for greater #include "todo.h" int main(int argc, char* argv[]) { using namespace std; ifstream in(argv[1]); typedef priority_queue< vector, greater > ToDo_List; ToDo_List td; while (in.good()) { to_do buf; in >> buf; td.push(buf); // sorts as it stores } while (td.size()) { // get each item, in sorted order: cout << td.top() << endl; td.pop(); } return 0; }