//File: sortIter.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; while (in>>item) v.push_back(item); } template void write_out (ostream & out, const vector & v) { vector::const_iterator iter; for (iter=v.begin(); iter != v.end(); ++iter) out << *iter << endl; out << endl; } template void my_swap (T & a, T & b) { T temp = a; a = b; b = temp; } template void sort (vector & v) { vector::iterator i; vector::iterator j; for (i = v.end()-1; i != v.begin(); i--) for (j = v.begin(); j != i; j++) { if (*j > *(j+1)) { //swaps v[j] and v[j+1] my_swap (*j, *(j+1)); } } }