【问题标题】:How to get a list of objects with common attributes?如何获取具有共同属性的对象列表?
【发布时间】:2021-11-30 06:27:28
【问题描述】:

给定一个类 Movie (id, title, ranking, release date, character number, ticket price, comment etc.)enum type: ACTION, COMEDY, DRAMA, FANTASY etc. 和一个类 Cinema (name, location)。我需要编写一个函数calculateProfit ( Movie*, day),它会根据某个特定日期计算电影院的利润。我还需要编写一种基于某些参数选择电影的方法,并根据发行日期对电影进行排序。我已经考虑这个问题几天了,但似乎我无法编写正确的代码。

为了能够根据参数选择电影,我需要获取具有相同特定属性的 Movie 类的所有对象的列表。我该怎么做?

这是我的课程的简要模板:

using namespace std;

class Movie{
public:
    int ID;
    string Title;
    int Ranking;
    string ReleaseDate;
    int CharacterNumber;
    int TicketPrice;
    string Comment;
    //SortingByDate
    enum type{
        ACTION, COMEDY, DRAMA, FANTASY
    } Type;
    Movie(int, string, int, string, int, int, string, type);
    Movie();
};
Movie::Movie(int ID, string Title,int Ranking,string ReleaseDate,int CharacterNumber, int TicketPrice,string Comment, type Type){
    this->ID=ID;
    this->Title=Title;
    this->Ranking=Ranking;
    this->ReleaseDate=ReleaseDate;
    this->CharacterNumber=CharacterNumber;
    this->TicketPrice=TicketPrice;
    this->Comment=Comment;
    this->Type=Type;  
class Cinema{
private:
    int calculateProfit();
public:
    //Vector with objects of Movie class
    string name;
    string location;

};

【问题讨论】:

  • "//Vector with objects of Movie class"std::vector<Movie> movies; 似乎不见了。
  • 对于您班级的教授、班级的教师助理或与您的同学的合作来说,这听起来像是一个很好的问题。
  • 您能不能至少给我们一个示例查询?这是您今天迄今为止提出的第三个问题,除了一般问题陈述之外,您的输入为零。您的老师是否希望您过滤一个属性?五?有什么属性吗?什么样的过滤?相等还是字符串匹配?您希望如何处理结果?
  • 我仍然不明白如何计算利润。 Movie 没有包含足够的信息来计算给定日期的利润。

标签: c++ oop methods attributes


【解决方案1】:

给定一个std::vector<std::shared_ptr<Movie>>,你可以按如下标题找到:

using MovieCollection = std::vector<std::shared_ptr<Movie>>;
MovieCollection find_by_title(const MovieCollection& collection, const std::string& fragment) {
  MovieCollection ret;
  for (auto movie: collection) {
    if (movie->title.find(fragment) != std::string::npos) {
      ret.push_back(movie);
    }
  }
  return ret;
}

【讨论】:

    猜你喜欢
    • 2021-08-11
    • 2012-04-18
    • 1970-01-01
    • 2022-06-11
    • 2013-01-28
    • 1970-01-01
    • 2021-10-12
    • 2011-01-17
    • 1970-01-01
    相关资源
    最近更新 更多