//File: sortVector.cpp #include #include #include //The following pragma will disable the 4 Microsoft warning messages //that occur with this implementation. These warnings refer to code //limited to the vector class and the main() function. #pragma warning(disable: 4786) // "identifier was truncated to '255' characters #include // in the debug information" using namespace std; template void read_in (istream & in, vector & v); template void write_out (ostream & out, const vector & v); template void my_swap (T & a, T & b); template void sort (vector & v); int main() { vector s; vector i; cout << "What type do you wish to sort? (S) strings (I) integers "; char reply; cin >> reply; ofstream fout ("out.txt"); cout << "What is the name of your input file? " << endl; string filename; cin >> filename; ifstream fin (filename.c_str()); switch (reply) { case 'S': case 's': read_in (fin, s); sort (s); write_out (fout, s); break; case 'I': case 'i': read_in (fin, i); sort (i); write_out (fout, i); break; default: cout << "Must choose either (S) or (I) " << endl; } return 0; } template void read_in (istream & in, vector & v) { T item; int n=0; while (in>>item) { v.resize(++n); //Vectors allow dynamic sizing of the list! v[n-1]=item; } } template void write_out (ostream & out, const vector & v) { for (int k=0; k void my_swap (T & a, T & b) { T temp = a; a = b; b = temp; } template void sort (vector & v) { for (int i = v.size()-1; i>0; i--) for (int j=0; j v[j+1]) { //swaps v[j] and v[j+1] my_swap (v[j], v[j+1]); } } }