1
我正在寫一個電影程序。 findFilmsByRating需要返回具有指定等級或更低等級的電影集。例如,調用具有FILM_RATING_PG_13參數的findFilmsByRating應該返回具有等級PG 13,PG和G的所有電影。任何幫助將被感激。如何在矢量圖中進行線性搜索?
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Film {
int id; // the unique id of the film
string name; // the film's name
string description; // the film's description
int runningTime; // the film's running time in minutes <= 180
int rating; // the film's rating, from the const decls
Film& operator =(const Film& bok);
bool operator ==(const Film& bok);
};
const int FILM_RATING_G = 0;
const int FILM_RATING_PG = 1;
const int FILM_RATING_PG_13 = 2;
const int FILM_RATING_R = 3;
const int FILM_RATING_NC_17 = 4;
const int FILM_RATING_UNRATED = 5;
bool Film::operator ==(const Film& bok)
{
return (runningTime == bok.runningTime&&rating == bok.rating&&name ==
bok.name&&description == bok.description);
}
Film& Film::operator=(const Film& bok)
{
if (*this == bok)
return *this;
else
{
runningTime = bok.runningTime;
description = bok.description;
name = bok.name;
rating = bok.rating;
}
return *this;
}
//function Def
void print(const vector<Film> &films);
vector <Film> selectionSort(vector <Film> films);
int find_smallest(const vector<Film>& vec, int index);
void my_swap(Film& obj_one, Film& obj_two);
vector <Film> findFilmsByRating(int rating, vector <Film> films);
string filmRatingToString(int rating);
vector <Film> findFilmsByRatingAndRunningTime(int rating, int runningTimes
vector <Film> films);
int main()
{
vector<Film> films;
films.push_back(Film());
films[0].id = 0;
films[0].name = "Three Kings";
films[0].description = "Army soldiers steal lots of gold";
films[0].runningTime = 114;
films[0].rating = FILM_RATING_R;
films.push_back(Film());
films[1].id = 1;
films[1].name = "Eagle Eye";
films[1].description = "Threatening their lives and family, the unseen caller uses everyday technology to control their actions and push them into increasing danger. ";
films[1].runningTime = 118;
films[1].rating = FILM_RATING_PG_13;
films.push_back(Film());
films[2].id = 2;
films[2].name = "Blade Runner";
films[2].description = "A Blade Runner must pursue and try to terminate four replicants";
films[2].runningTime = 117;
films[2].rating = FILM_RATING_R;
films.push_back(Film());
films[3].id = 3;
films[3].name = "Limitless";
films[3].description = "A mysterious pill helps a man use 100% of his brain";
films[3].runningTime = 105;
films[3].rating = FILM_RATING_PG_13;
films.push_back(Film());
films[4].id = 4;
films[4].name = "Inception";
films[4].description = "Invading dream and changeing thoughts";
films[4].runningTime = 105;
films[4].rating = FILM_RATING_PG_13;
films.push_back(Film());
films[5].id = 5;
films[5].name = "Twilight";
films[5].description = "Sparkly vampires";
films[5].runningTime = 122;
films[5].rating = FILM_RATING_PG_13;
films.push_back(Film());
films[6].id = 6;
films[6].name = "Twilight New Moon";
films[6].description = "Sparkly vampires, and now a hot warewolf.";
films[6].runningTime = 130;
films[6].rating = FILM_RATING_PG_13;
films.push_back(Film());
films[7].id = 7;
films[7].name = "The lovely bones";
films[7].description = "Murder victim helps find killer from otherside.";
films[7].runningTime = 122;
films[7].rating = FILM_RATING_PG_13;
films.push_back(Film());
films[8].id = 8;
films[8].name = "Promethis";
films[8].description = "Pre-aleins";
films[8].runningTime = 124;
films[8].rating = FILM_RATING_R;
films.push_back(Film());
films[9].id = 9;
films[9].name = "The Hunger Games";
films[9].description = "Girl gets taken from home to fight to the death for rich people.";
films[9].runningTime = 142;
films[9].rating = FILM_RATING_PG_13;
films.push_back(Film());
films[10].id = 10;
films[10].name = "Baby Driver";
films[10].description = "Kid with headphone drives the get away car.";
films[10].runningTime = 113;
films[10].rating = FILM_RATING_R;
print(films);
films = selectionSort(films);
int rating = 0;
films = findFilmsByRating (rating,films);
cout << "****List of Movies by rating******" << endl;
for (int i = 0; i < films.size(); ++i)
{
cout << endl;
cout << "Film title: " << films[i].name << endl;
cout << "Film description: " << films[i].description << endl;
cout << "Film runtime: " << films[i].runningTime << endl;
cout << "Film rating: " << films[i].rating << endl;
}
system("PAUSE");
}
void print(const vector<Film> &films)
{
cout << "****List of Movies******" << endl;
for (int i = 0; i < films.size(); ++i)
{
cout << endl;
cout <<"Film title: "<< films[i].name << endl;
cout <<"Film description: "<< films[i].description << endl;
cout <<"Film runtime: "<<films[i].runningTime << endl;
cout <<"Film rating: "<< films[i].rating << endl;
}
}
vector<Film> selectionSort(vector<Film> films)
{
for (unsigned int count = 0; count <films.size(); count++)
{
int ind_to_swap = find_smallest(films, count);
my_swap(films[count], films[ind_to_swap]);
}
cout << "******** List by Run time *********" << endl;
for (int i = 0; i < films.size(); ++i)
{
cout << endl;
cout << "Film title: " << films[i].name << endl;
cout << "Film description: " << films[i].description << endl;
cout << "Film runtime: " << films[i].runningTime << endl;
cout << "Film rating: " << films[i].rating << endl;
}
return vector<Film>();
}
int find_smallest(const vector<Film>& vec, int index)
{
int smallest_index = index;
Film temp = vec[index];
for (unsigned int i = index; i <vec.size(); i++)
{
if (vec[i].runningTime <temp.runningTime)
{
smallest_index = i;
temp = vec[i];
}
}
return smallest_index;
}
void my_swap(Film & obj_one, Film & obj_two)
{
Film temp = obj_one;
obj_one = obj_two;
obj_two = temp;
}
vector<Film> findFilmsByRating(int rating, vector<Film> films)
{
int nIndex;
for (nIndex = 0; nIndex < films.size(); nIndex++)
{
if (rating == films[nIndex].rating)
{
return vector<Film>(rating);
}
}
return vector<Film>();
}
無關:查找[列表初始化](http://en.cppreference.com/w/cpp/language/list_initialization)可以更簡單地加載該向量。例如:https://ideone.com/8E65eU – user4581301