【发布时间】:2016-02-15 02:53:11
【问题描述】:
我正在尝试按对象的 1 个属性(3 个字符串、1 个 int、1 个浮点数)对对象数组进行排序。我需要按它们的整数,从高到低,然后按它们的字符串,按字母顺序对它们进行排序。我无法理解如何仅访问对象的一部分。
这是我所有的代码,我已经包含了一些用于排序的示例代码。
#include<iostream>
using namespace std;
#include "video.h"
int main() {
const int MAX = 100;
Video *video[MAX]; //up to 100 videos
for(int l = 0; l < MAX; l++)
{
video[l] = NULL;
// cout << video[l] << endl; //testing if all are equal to 0
}
string title;
string url;
string desc;
string sorting;
float length;
int rate;
// cout << "What sorting method would you like to use?" << endl;
getline(cin, sorting);
//cout << "Enter the title, the URL, a comment, the length, and a rating for each video" << endl;
int t = 0;
while(getline(cin, title))
{
getline(cin, url);
getline(cin, desc);
cin >> length;
cin >> rate;
cin.ignore();
video[t] = new Video(title, url, desc, length, rate);
t++;
}
for(int s = 0; s < t; s++){
video[s]->print();
}
for(int e = 0; e < t; e++)
{
delete video[e];
}
// SORTING
if(sorting == "length") {
int q = 0;
bool Video::longer(Video *video)
{ return m_length > other->m_length; }}
else if(sorting == "rating") {
}
else if(sorting == "title") {
for(int r = 0; r < MAX; r++) {
}
else{
cerr << sorting << " is not a legal sorting method, giving up" << endl;
return 1; }
//this sorts the videos by length
for(int last = num_videos -1; last > 0; last--) {
for(int cur = 0; cur < last, cur++) {
if(videos[cur]->loner(videos[cur+1])) {
swap(videos[cure], videos[cur+1]); }}}
【问题讨论】:
-
现有很多类似的问题(有答案)——例如here。如果您尝试采用这种方法并遇到困难,请展示您的代码并记录您遇到的具体问题。
-
我浏览了很多关于此的帖子,但没有一篇文章以对我有意义的方式解释了实际过程,并且可以应用于我的特定程序。
-
如果你能展示你做了什么,那就太好了,只需添加你当前的代码,我们可以帮助你看看哪里错了
-
为了让其他 StackOverflow 用户了解什么对您有意义并且可以应用于您的特定程序,我建议您提供足够的信息。例如,显示您的代码并突出显示缺失的部分。
-
这是你今天就这段代码提出的第三个问题。