//File:	sortIter.cpp
#include <iostream>
#include <fstream>
#include <string>

//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 <vector>		//  in the debug information"

using namespace std;

template<class T> void read_in (istream & in, vector<T> & v);
template<class T> void write_out (ostream & out, const vector<T> & v);
template<class T> void my_swap (T & a, T & b);
template<class T> void sort (vector<T> & v);

int main()
{
	vector<string> 	s;
	vector<int>	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<class T> void read_in (istream & in, vector<T> & v)
{
	T item;
	while (in>>item)
		v.push_back(item);
}

template<class T> void write_out (ostream & out, const vector<T> & v)
{
	vector<T>::const_iterator	iter;
	for (iter=v.begin(); iter != v.end(); ++iter)
		out << *iter << endl;
	out << endl;
}

template<class T> void my_swap (T & a, T & b)
        {
                T temp = a;
                a = b;
                b = temp;
        }
        
template<class T> void sort (vector<T> & v)
	{
		vector<T>::iterator	i;
		vector<T>::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));
				}
			}
	}
