如何比较和排序 class 中的特定参数?

How do you compare and sort a specific parameter within a class?

我的教授要求我们制作一个程序,接受用户的输入并继续阅读直到输入结束。只有这样,程序才能输出用户输入的内容。

输入应基于视频标题,即 url、对视频的评论、时长(以分钟为单位)和评分(以 * 为单位)。

例如:

United Break Guitars, https://www.youtube.com/watch?v+5YGc4zOqozo, Great example of one person getting a giant company to listen, 4.5, ***, Space Versus Tabs, https://www.youtube.com/watch?v=SsoOG6ZeyUl, Decide for yourself: spaces or tabs?, 2.83, ****

在解释之前,我已经完成并测试了一切是否正常。我的问题是项目的下一部分要求用户在 Rating, Length, or title 之间进行选择,然后根据用户的选择对它们进行排序。 如果我选择评分,那么上面的输入应该从评分最高的视频到最低的排序。

这是我目前所拥有的:

#include <iostream>
#include <stdlib.h>
#include <cstring>
#include <algorithm>
using namespace std;

#include "video.h"

int main()
{
string user, url, comment, title;
int rating;
double length;
int i = 0, last = 0;

Video *videoObj[100];
Video *temp[100];


// specifies how the videos should be sorted
cin >> user;
cin.ignore();


while (getline(cin,title)  ) {


getline(cin, url);
getline(cin, comment);
cin >> length;
cin >> rating;
cin.ignore();



videoObj[i] = new Video(title, url, comment, length, rating);
i++;
last++;
}

temp[i] = new Video(title, url, comment, length, rating);

if(user=="rating"){

    for(int i = 0; i < last - 1; i++){

for(int j = i+1; j< last; j++){

    if(videoObj[i] -> Rating(videoObj[j])) {

        temp[i] = videoObj[i];
        videoObj[i]= Rating(videoObj[j]);
        Rating(videoObj[j]) = temp[i];
      }
     }
    }
   }

for(int i= 0; i < last; i++)
{
videoObj[i]->print();

}

 //delete[] videoObj;



return 0;
}

video.cpp 文件:

#include <iostream>
#include <algorithm>
using namespace std;

#include "video.h"

Video::Video(string video_title, string video_link, string video_comment, double video_length, int video_number)
: title(video_title), link(video_link), comment(video_comment), length(video_length), rating(video_number)
{
}

bool Video::Rating(Video *videoObj) {
    if(rating > videoObj-> rating )
    {
        return true;
    }
    else
    {
        return false;
    }

}

void Video::print(){

string star;
switch(rating){

case 1:
    star = "*";
    break;
case 2:
    star = "**";
    break;
case 3:
    star = "***";
    break;
case 4:
    star = "****";
    break;
case 5:
    star = "*****";
    break;

}



 cout << title << ", " << link << ", " << comment << ", " << length << ", " << star << endl;

}



void Video::temp(){
title, link, comment, length, rating;
}

video.h 文件:

#ifndef VIDEO_H
#define VIDEO_H

using namespace std;

class Video {

public:
    Video(string video_title, string video_link, string video_comment, double video_length, int video_number);
    void print();
    bool Rating(Video *videoObj);
    void temp();
   

private:
    string title;
    string link;
    string comment;
    double length;
    int rating;

};


#endif

老实说,我不知道如何正确实施冒泡排序。我在 youtube 上查找了多个不同的视频并在 Whosebug 上发布了帖子,但我似乎无法弄清楚如何在 class.

中对特定参数进行排序

我的教授给了我们这些在 class:

中进行排序的说明

When sorting the videos you need to be able to determine how two video objects should be ordered. The easiest way to do this is to write member functions to handle the comparisons in class Video. For example, this method could be used when sorting the videos by length:

// return true if the current video is longer than the given video (other) ,
// otherwise return false
bool Video :: longer(Video *other) {
 return (mlength > other -> mlength ;
}

我什至不确定我是否在我的 video.cpp 文件中正确地完成了那部分。关于如何使排序方法正常工作的任何想法? 请温柔点,我对编程很陌生。我意识到我的冒泡排序也是错误的,我只是不知道从哪里开始修复它...

我通常将 std::sort 与比较运算符一起用于您希望能够比较的每个字段。您可以将它们命名为 类:

struct by_title {
    bool operator()(Video const &a, Video const &b) { 
        return a.title < b.title;
    }
};

struct by_rating {
    bool operator()(Video const &a, Video const &b) {
        return a.rating < b.rating;
    }
};

// ...
std::sort(videos.begin(), videos.end(), by_rating);

std::sort(videos.begin(), videos.end(), by_title);

...或者您可以使用 lambda 表达式来定义比较:

// sort by rating
std::sort(videos.begin(), videos.end(), [](auto &a, auto &b) { return a.rating < b.rating; });

// sort by title
std::sort(videos.begin(), videos.end(), [](auto &a, auto &b) { return a.title < b.title; });