How to sort a class array

限于喜欢 提交于 2021-02-19 20:08:05

问题


I am trying to sort a class array that has 5 values inside of it. 3 strings and 2 ints. I would like to sort the array from highest to lowest on the int values but can't figure out how to do so. My though process is to send the array into the class and then pull out the correct int value and sort for each array location without changing the other values of that location. How can I pull out these values so that I can sort them accordingly? If I could then I would know how to finish my code. If there is an easier way to do this then I am open to any suggestions.

In the code below I have a template of what I would do if I could pull that number out:

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

class Thing
{
public:
  Thing();
  void setvariables(string s, string g, string a, int y, int l);
  void get();
  void print();
  void sort_time(Thing data[], int datasize);

private:
  string name;
  string genre;
  string artist;
  int year;
  int length;
};

Thing::Thing()
{
  name = "";
  genre = "";
  artist = "";
  year = 13;
  length = 15;
}

void Thing::setvariables(string n, string g, string a, int y, int l)
{
  name = n;
  genre = g;
  artist = a;
  year = y;
  length = l;
}
/* void Thing::sort_time(Thing data[], int datasize)
{
int lar_pos, pos, lar_val;
for (int index = 0; index < datasize; index++)
{
    lar_pos = index;
    lar_val = data[index].get();
    for (pos = index; pos < datasize; pos++)
    {
        if (data[pos] > data[lar_pos])
        {
            lar_pos = pos;
            lar_val = data[lar_pos];
        }
    }
    data[lar_pos] = data[index];
    data[index] = lar_val;
}
}
void Thing::get()
{
l = length;
}
*/
void Thing ::print()
{
    cout << setw(25) << name << setw(10) << genre << setw(5) << year
    << setw(30) << artist << setw(5) << length << endl;
}

int main()
{
  // Create array of things
  int size = 9;
  Thing array[9];

  // Initialize array of things
  for (int i = 0; i<size; i++)
  {
    string name, genre, artist, junk;
    int year, length;

    getline(cin, name);
    getline(cin, genre);
    getline(cin, artist);
    cin >> year;
    cin >> length;
    array[i].setvariables(name, genre, artist, year, length);
    cin.ignore(256, '\n');
  }

  // Print array of things
  cout << setw(25) << "TITLE" << setw(10) << "GENRE" << setw(5) << "YEAR"
     << setw(30) << "ARTIST" << setw(5) << "TIME" << endl;
  cout << setw(25) << "=====" << setw(10) << "=====" << setw(5) << "===="
     << setw(30) << "======" << setw(5) << "====" << endl;
  for (int i = 0; i<size; i++)
    array[i].print();
  return 0;
}

回答1:


Use std::sort with a custom comparator or define operator< for your type.

Example:

#include <algorithm>

class Thing
{
  // ...
};

class ThingComparator
{
  bool operator()(const Thing& a, const Thing& b)
  {
    // Define your logic here and return true if a is considered lesser than b.
  }
};

int main()
{
  const int size = 9; // Make it constant!!
  Thing array[size];
  // Fill the array ...

  // Then sort it
  std::sort(array, array + size, ThingComparator()); 
}


来源:https://stackoverflow.com/questions/20363682/how-to-sort-a-class-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!