//File:	sortVector.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;
	int n=0;
	while (in>>item)
	{
		v.resize(++n);	//Vectors allow dynamic sizing of the list!
		v[n-1]=item;
	}
}

template<class T> void write_out (ostream & out, const vector<T> & v)
{
	for (int k=0; k<v.size(); k++)
	{
		out << v[k] << 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)
	{
		for (int i = v.size()-1; i>0; i--)
			for  (int j=0; j<i; j++)
			{
				if (v[j] > v[j+1])
				{
					//swaps v[j] and v[j+1]
					my_swap (v[j], v[j+1]);
				}
			}
	}
