1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
#include <iostream>
#include <set>
#include <string>
#include <map>
#include <vector>
#include <iterator>
#include <algorithm>
#include <fstream>
using namespace std;
void doP(set<string>);
ostream& operator<< (ostream& out, set<string> &any);
bool compare(const pair<string, double> &, const pair<string, double> &);
set<string> criticsPick(map<string, double>);
int main()
{
map<string, double> movieList;
string fname = "Movies.txt";
ifstream myfile;
myfile.open(fname);
if (myfile.is_open())
{
cout << "File Opened" << endl;
}
else
{
cout << "BAD : " + fname << endl;
return -99;
}
string nameString = "";
string scoreString = "";
while (myfile)
{
getline(myfile, nameString);
getline(myfile, scoreString);
movieList[nameString] = atof(scoreString.c_str());
}
//map<string,double>::iterator it = movieList.begin();
//while(it != movieList.end())
//{
// std::cout<<it->first<<" :: "<<it->second<<std::endl;
// it++;
//}
set<string> results;
results = criticsPick(movieList);
for_each(results.begin(), results.end(), doP);
system("pause");
}
void doP(set<string> i)
{
cout << i << endl;
}
ostream& operator<< (ostream& out, set<string> &list)
{
out << list;
return out;
}
bool compare(const pair<string, double> &left, const pair<string, double> &right)
{
return left.second > right.second;
}
set<string> criticsPick(map<string, double> list)
{
set<string> setList;
vector<pair<string, double>> vectorList;
copy(list.begin(), list.end(), back_inserter(vectorList));
sort(vectorList.begin(), vectorList.end(), compare);
/*for (int i = 0; i < 10; i++)
{
cout << vectorList[i].first << vectorList[i].second << endl;
}*/
for (int i = 0; i < 10; i++)
{
setList.insert(vectorList[i].first);
}
/*for (set<string>::iterator iter = setList.begin(); iter != setList.end(); ++iter)
cout << *iter << endl;*/
return setList;
}
|