排序功能不起作用
Sorting function isn't working
编辑:好的,我已经换成交换,但是它似乎是根据标题而不是长度交换。这是当前的输出:
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]))
以前的条件将把较长长度的视频推向末尾。在这里,我们想将较短的视频推到最后。
相关文章:
- 为什么我的打印功能不起作用?链表
- Cuda 基本程序 (将值写入矩阵和 std:cout 不起作用) ;主功能不启动
- 我做错了什么?GetDiskFreeSpaceExA的功能根本不起作用
- 虚拟功能不起作用
- C++插入排序错误功能不起作用
- 使用链表.为什么我的插入功能不起作用?
- 功能指针不起作用(C )
- 为什么功能对我不起作用?C
- C 所有质数功能不起作用
- 朋友功能不起作用(语法错误)
- Winpcap:暂停/停止嗅探pcap_breakloop功能不起作用
- 我的模板功能具有通用引用不起作用
- C 类是功能不起作用的参数
- 现在将功能从主型到原型不起作用
- 排序功能不起作用
- TIM6,基本时间生成功能不起作用
- 功能调用不起作用,但控制台仍允许输入
- 二进制树级别明智的打印功能不起作用
- glutkeyboardfunc呼叫功能不起作用
- 为什么COUT在朋友函数中不起作用,该功能超载了操作员&lt;&lt;这是一个iStream运算符