#include #include #include "vector.h" #include "CPstring.h" // file: tagsort.cc // author Dietolf Ramm; date: 12/5/96 // illustrate new // illustrate use of vector of pointers struct who { string name; int zip; }; void GetData(Vector & tags, int & size) // precondition: Empty vector of pointers to who: tags // File "TagData" with lines containing a zipcode followed by a name // postcondition: struct of type who allocated for each line in // file "TagData" with zip and name stored. Vector tags contains // pointers to each of the structs. size contains number of structs // (== number of vector elements used) { int code, k=0; ifstream datain("TagData"); while ( datain >> code) { tags[k] = new who; tags[k] -> zip = code; getline(datain, tags[k] -> name); k++; } size = k; } void ShowData(const Vector & tags, int size) // precondition: tags contains pointers to size structs containing // data fields zip and name. // postcondition: zip and name fields printed out in order dictated // by the order of the pointers in tags { int k; for (k = 0; k < size; k++) { cout << tags[k]->name << ", " << tags[k] -> zip << endl; } } void SelectTagSortZip(Vector & tags, int size) // precondition: tags contains pointers to size structs containing // data fields zip and name. // postcondition: tags entries sorted so as to point to structs // in ordeer by zip { int k, j, minindex; who * temp; for (k = 0; k < size -1; k++) { minindex = k; for (j = k+1; j < size; j++) if (tags[j] -> zip < tags[minindex] -> zip) minindex = j; temp = tags[k]; tags[k] = tags[minindex]; tags[minindex] = temp; } } void SelectTagSortName(Vector & tags, int size) // precondition: tags contains pointers to size structs containing // data fields zip and name. // postcondition: tags entries sorted so as to point to structs // in ordeer by name { int k, j, minindex; who * temp; for (k = 0; k < size -1; k++) { minindex = k; for (j = k+1; j < size; j++) if (tags[j] -> name < tags[minindex] -> name) minindex = j; temp = tags[k]; tags[k] = tags[minindex]; tags[minindex] = temp; } } int main() { Vector tags(100); int size = 0; GetData(tags, size); cout << "From file:" << endl; ShowData(tags, size); SelectTagSortZip(tags, size); cout << "Sorted by zip:" << endl; ShowData(tags, size); SelectTagSortName(tags, size); cout << "Sorted by name:" << endl; ShowData(tags, size); return 0; } Sample output: From file: Michael Ramm, 22301 Nicholas Ramm, 22301 Lenore Ramm, 27278 Karl Ramm, 27701 Barbara Ramm, 22301 Eberhard Ramm, 37212 Wolfhard Ramm, 22301 Hartmut Ramm, 30582 Mary-Kathlyn Ramm, 27278 Dietolf Ramm, 27278 Sieglind Ramm, 80802 Ruth Ramm, 37355 Dora Ramm, 27705 Heinrich Ramm, 37355 Sorted by zip: Michael Ramm, 22301 Nicholas Ramm, 22301 Barbara Ramm, 22301 Wolfhard Ramm, 22301 Lenore Ramm, 27278 Mary-Kathlyn Ramm, 27278 Dietolf Ramm, 27278 Karl Ramm, 27701 Dora Ramm, 27705 Hartmut Ramm, 30582 Eberhard Ramm, 37212 Ruth Ramm, 37355 Heinrich Ramm, 37355 Sieglind Ramm, 80802 Sorted by name: Barbara Ramm, 22301 Dietolf Ramm, 27278 Dora Ramm, 27705 Eberhard Ramm, 37212 Hartmut Ramm, 30582 Heinrich Ramm, 37355 Karl Ramm, 27701 Lenore Ramm, 27278 Mary-Kathlyn Ramm, 27278 Michael Ramm, 22301 Nicholas Ramm, 22301 Ruth Ramm, 37355 Sieglind Ramm, 80802 Wolfhard Ramm, 22301