Hi, my program works fine but its not very efficient. It struggles with large files. I was pretty proud of myself that i managed to code it well,but is there anyway of making it better?
this could help me become a better programmer...
kind regards
JoHN
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <algorithm>
#include <vector>
using namespace std;
typedef vector<int> vect_int;
map<string,vect_int*> *ptMap; // need global ptr to use in std::sort compare predicate.
string readFile();
bool sort_map_greater( string item1, string item2)
{
return (*ptMap)[item1]->size()>(*ptMap)[item2]->size();
}
int main( int argc, char *argv[]) //
{
// TODO:check that filename was given as command line argument here
char charbuf;
//string sequence;
//string inputfile(argv[1]);
map<string, vect_int*> repeats;
vector<string> index; // to be used for sorting
ptMap = &repeats;
string sequence(readFile());
// TODO: add functionality to input get k
// get topmost
int k;
cout << "Enter k: ";
cin >> k;
//TODO: check valid input
cout << "Enter top n repeats to show: ";
int topmost;
cin >> topmost;
//TODO: check for valid input
for (int i = 0;i<sequence.size()-k;++i)
{
string sub = sequence.substr(i,k);//get substring based on k size;
if (repeats.find(sub)==repeats.end())// have we already mapped it?
{
//no, then add key with new vector
repeats[sub]=new vector<int>;
index.push_back(sub);//also add string to our index while we are at it.
}
repeats[sub]->push_back(i);// add position to hash
}
// ok now that we got them in
// need to sort them to get topmost
sort(index.begin(), index.end(),sort_map_greater);//std::sort makes things easy
//let's output the result
for (int i = 0; (i < topmost) && (i < index.size()); i++)
{
cout << "Number of repeats in order: " << index[i] << " with " << repeats[index[i]]->size() << endl;
for (vector<int>::iterator it = repeats[index[i]]->begin();it != repeats[index[i]]->end();it++)
{
cout << *it << " ";
}
cout << endl;
}
//finally we need to delete all those new vectors
for (map<string, vect_int*>::iterator mIt = repeats.begin(); mIt!= repeats.end();mIt++)
{
delete mIt->second;
}
}
/*****************************************************************************/
string readFile() {
string filename;
int j;
string information;
char tmp = 0;
string data("");
cout << "Enter filename: ";
cin >> filename;
ifstream infile(filename.c_str());
for(j=0; j < 3; j++)
{
cout << "." << endl;
}
if( !filename.c_str() )
{
cout << filename.c_str() << " has not been loaded " << endl;
cin.get();
}
else
{
cout << filename.c_str() << " has been loaded" << endl;
}
while(!infile.eof()){
infile.get(tmp);
tmp= toupper(tmp);
if ((tmp == 'A') || (tmp == 'T') || (tmp == 'C') || (tmp == 'G'))
{
data += tmp;
}
}
return data;
}