排序功能不起作用

Sorting function isn't working

本文关键字:不起作用 功能 排序      更新时间:2023-10-16

编辑:好的,我已经换成交换,但是它似乎是根据标题而不是长度交换。这是当前的输出:

Title3, Url3, Comment3, 5.8, ***
Title2, Url2, Comment2, 9.3, ****
Title1, Url1, COmment1, 2.3, ****

这就是当前功能的样子:

if (sort == "length") { //order based on length
    for (int last = size -1; last > 0; last --) 
    {
        for (int cur = 0; cur < last; cur ++) 
        {
            if (!videos[cur]->longer(videos[cur+1])) {
                cout << "it's swapping" << endl;
                swap(videos[cur], videos[cur+1]);
            }
        }
    }
}

我正在尝试创建一系列视频,包括视频标题,URL,评论,视频的长度和评分。我还允许用户按长度,评分或标题对视频进行排序。目前,我只是试图按长度对其进行排序,这是不起作用的。这是我的输入:

length
Title1
http://www.youtube.com
Comment ONE
2.3
4
Title2
http://www.youtube.com
Comment TWO
9.4
2
Title3
http://www.youtube.com
Comment THREE
5.7
3

这是我当前的输出:

Title1, http://www.youtube.com, Comment ONE, 2.3, ****
Title2, http://www.youtube.com, Comment TWO, 9.4, **
Title3, http://www.youtube.com, Comment THREE, 5.7, ***

这是我的输出应该是:

Title2, http://www.youtube.com, Comment TWO, 9.4, **
Title3, http://www.youtube.com, Comment THREE, 5.7, ***
Title1, http://www.youtube.com, Comment ONE, 2.3, ****

由于9.4是最大的长度,因此应该在顶部。由于2.3是最短的长度,因此应该在底部。

int main()
{
    const int MAX = 100; //setting max value
    Video * videos[MAX]; //100 limit
    int size = 0; //initalizing "size" at 0.
    string titletemp; 
    string linktemp; 
    string commenttemp; 
    double lengthtemp; 
    int ratingtemp;
    string sort;
    cin >> sort; //sorting preference
    if ((sort != "length") && (sort != "rating") && (sort != "title")) { //checking to see if they used the right input
        cerr << sort << " is not a legal sorting method, giving up." << endl;
        return 1;
    }
    cin.ignore();
    for (int i = 0; i < MAX; i++) 
    {
        getline(cin, titletemp); //getting title from user
        if ( cin.eof() ) { //breaking if ctrl + d is used
            break;
        } else if ( !cin ) {
            cin.clear();
            cin.ignore();
        }
        getline(cin, linktemp); //repeating for all other fields
        if ( cin.eof() ) {
            break;
        } else if ( !cin ) {
            cin.clear();
            cin.ignore();
        }
        getline(cin, commenttemp);
        if ( cin.eof() ) {
            break;
        } else if ( !cin ) {
            cin.clear();
            cin.ignore();
        }
        cin >> lengthtemp;
        if ( cin.eof() ) {
            break;
        } else if ( !cin ) {
            cin.clear();
            cin.ignore();
        }
        cin >> ratingtemp;
        if ( cin.eof() ) {
            break;
        } else if ( !cin ) {
            cin.clear();
            cin.ignore();
        }
        cin.ignore();
        videos[i] = new Video(titletemp, linktemp, commenttemp, lengthtemp, ratingtemp);
        size++; //increase the size of array, assuming the for loop hasn't been broken
    }   
    if (sort == "length") { //order based on length
        for (int last = size -1; last > 0; last --) 
        {
            for (int cur = 0; cur < last; cur ++) 
            {
                if (videos[cur]->longer(videos[cur+1])) {
                    swap(videos[cur], videos[cur+1]);
                }
            }
        }
    }
    for (int i = 0; i < size; i++) //print all the ordered videos
    {
        videos[i]->print();
    }
    return 0;
}

这是来自video.cpp文件的"更长"函数:

bool Video::longer(Video *other)
{
    return m_length > other->m_length;
}

m_length是标题文件中的双指指针。

double * m_length;

有人知道为什么这不是分类吗?谢谢!

第一个问题在于 length是指针。将其仅仅是double而不是double *double *将其作为地址存储。只有double可以正常工作。

我认为编译器应该警告将double分配给double *

对于上述,更改

double * m_length;

to

double m_length;

以下条件应为

if (videos[cur]->longer(videos[cur+1]))

应该是

if (!videos[cur]->longer(videos[cur+1]))

以前的条件将把较长长度的视频推向末尾。在这里,我们想将较短的视频推到最后。