// file: stlex1.cc // author: keller // purpose: simple STL examples // // RCS: $Id: stlex1.cc,v 1.1 2000/02/09 08:39:48 keller Exp $ // #include #include #include main() { // Create an array of strings. string word[] = {"The", "quick", "brown", "fox", "just", "plays"}; // Compute the number of elements. int numElements = sizeof(word)/sizeof(char*); // STL stuff starts here. // Copy the array into a list list s; for( int i = 0; i < numElements; i++ ) { s.push_back(word[i]); // push on "back" (end) of list } // Output the original array, using STL algorithm copy and ostream_iterator. cout << "Original array: "; copy(word, word+numElements, ostream_iterator(cout, ", ")); cout << endl; // Sort the array using STL algorithm sort and output it. sort(word, word+numElements); cout << "Sorted array: "; copy(word, word+numElements, ostream_iterator(cout, ", ")); cout << endl; // Output list of strings with a blank between each, but none at the end. // Add a period at the end. list::iterator p = s.begin(); // point to beginning of list if( p != s.end() ) { cout << *p; // output first string, if there is one p++; } while( p != s.end() ) { cout << " " << *p; // output other strings, with spacing p++; } cout << "." << endl; // output ending }