【问题标题】:Trouble Sorting an Array of Objects c++无法对对象数组进行排序c ++
【发布时间】: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 用户了解什么对您有意义并且可以应用于您的特定程序,我建议您提供足够的信息。例如,显示您的代码并突出显示缺失的部分。
  • 这是你今天就这段代码提出的第三个问题。

标签: c++ arrays sorting object


【解决方案1】:

你可以看看 std::sort

http://www.cplusplus.com/reference/algorithm/sort/

您可以创建自定义比较器来比较您希望比较的对象部分。

【讨论】:

    【解决方案2】:

    如果您想要任何超出默认值(使用运算符

    我们来上课吧:

    class Example
    {
    public:
      std::string thing1;
      std::string thing2;
      std::string thing3;
      int         int1;
      float       f1;
    };
    

    std::sort 函数将传递对两个对象的引用。如果第一个参数在第二个之前,比较函数需要返回 true

    bool comparison(const Example& a, const Example& b)
    {
      // Compare by their integers.
      if (a.int1 < b.int1)
      {
         return true;
      }
      if (a.int1 > b.int1)
      {
         return false;
      }
      // We can assume the integers are equal.
      // Now to compare by strings.
      if (a.thing1 != b.thing1)
      {
         return a.thing1 < b.thing1;
      }
      // The first strings are equal at this point.
      // Compare the second versus the second.
      // Which is left as an exercise for the reader.
      return false;
    }
    

    如果这些对象中有std::vector,则使用std::sort函数:

    std::vector<Example> many_examples;
    std::sort(many_examples.begin(), many_examples.end(), comparison);
    

    【讨论】:

      猜你喜欢
      • 2013-09-06
      • 1970-01-01
      • 1970-01-01
      • 2021-11-18
      • 1970-01-01
      • 2019-04-20
      • 2011-08-18
      相关资源
      最近更新 更多