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]);
}
}
}
}
我正在尝试创建一系列视频,包括视频标题,网址,评论,视频长度和评分。我还允许用户按长度,评级或标题对视频进行排序。现在我只是想按长度排序,这是行不通的。这是我的意见:
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;
有谁知道为什么这不是排序?谢谢!
答案 0 :(得分:1)
第一个问题在于length
是一个指针。只需double
代替double *
。 double *
会将其存储为地址。只需double
即可。
我认为编译器应该在将double
分配给double *
时发出警告。
对于上述内容,请更改
double * m_length;
到
double m_length;
以下条件应为
if (videos[cur]->longer(videos[cur+1]))
应该是
if (!videos[cur]->longer(videos[cur+1]))
以前的情况会将更长的视频推到最后。在这里,我们希望将更短的视频推到最后。