对自定义对象的向量进行排序

327

如何对包含自定义(即用户定义)对象的向量进行排序呢?
可能需要使用标准STL算法sort和一个谓词(一个函数或函数对象),该谓词将操作自定义对象中的一个字段(作为排序的键)。
我走在正确的道路上吗?


1
可能是标准库排序和用户定义类型的重复问题。 - MCCCS
15个回答

2
typedef struct Freqamp{
    double freq;
    double amp;
}FREQAMP;

bool struct_cmp_by_freq(FREQAMP a, FREQAMP b)
{
    return a.freq < b.freq;
}

main()
{
    vector <FREQAMP> temp;
    FREQAMP freqAMP;

    freqAMP.freq = 330;
    freqAMP.amp = 117.56;
    temp.push_back(freqAMP);

    freqAMP.freq = 450;
    freqAMP.amp = 99.56;
    temp.push_back(freqAMP);

    freqAMP.freq = 110;
    freqAMP.amp = 106.56;
    temp.push_back(freqAMP);

    sort(temp.begin(),temp.end(), struct_cmp_by_freq);
}

如果比较结果为假,它会执行“交换”操作。

在任何一种语言中都无法编译。 - L. F.

2
你可以使用用户定义的比较器类。
class comparator
{
    int x;
    bool operator()( const comparator &m,  const comparator &n )
    { 
       return m.x<n.x;
    }
 }

2
    // sort algorithm example
    #include <iostream>     // std::cout
    #include <algorithm>    // std::sort
    #include <vector>       // std::vector
    using namespace std;
    int main () {
        char myints[] = {'F','C','E','G','A','H','B','D'};
        vector<char> myvector (myints, myints+8);               // 32 71 12 45 26 80 53 33
        // using default comparison (operator <):
        sort (myvector.begin(), myvector.end());           //(12 32 45 71)26 80 53 33
        // print out content:
        cout << "myvector contains:";
        for (int i=0; i!=8; i++)
            cout << ' ' <<myvector[i];
        cout << '\n';
        system("PAUSE");
    return 0;
    }

1

如果要对向量进行排序,可以使用C++中的sort()算法。

sort(vec.begin(),vec.end(),less<int>());

第三个参数可以使用大于或小于任何函数或对象,但如果您将第三个参数留空,则默认运算符为<。

// using function as comp
std::sort (myvector.begin()+4, myvector.end(), myfunction);
bool myfunction (int i,int j) { return (i<j); }

// using object as comp
std::sort (myvector.begin(), myvector.end(), myobject);

1
由于这是关于这个问题的谷歌搜索结果中排名最高的,我将发布我偏爱的方法(与上面许多方法相似,但细节有所不同)。此外,这是一个可复制/粘贴并运行的示例。
// SortObjectByField.cpp

#include <iostream>
#include <vector>
#include <algorithm>

struct Meeting
{
  int startTime;
  int endTime;

  Meeting() { }
  Meeting(int startTime, int endTime)
  {
    this->startTime = startTime;
    this->endTime = endTime;
  }

  struct SortByStartTime
  {
    bool operator()(const Meeting& meeting1, const Meeting& meeting2)
    {
      return meeting1.startTime < meeting2.startTime;
    }
  };

  struct SortByEndTime
  {
    bool operator()(const Meeting& meeting1, const Meeting& meeting2)
    {
      return meeting1.endTime < meeting2.endTime;
    }
  };
};

// function prototypes
void printMeetings(const std::vector<Meeting>& meetings);

int main(void)
{
  std::vector<Meeting> meetings = { Meeting(5, 10), Meeting(15, 20), Meeting(2, 25) };

  std::sort(meetings.begin(), meetings.end(), Meeting::SortByStartTime());

  printMeetings(meetings);

  std::sort(meetings.begin(), meetings.end(), Meeting::SortByEndTime());

  printMeetings(meetings);

  return 0;
}

void printMeetings(const std::vector<Meeting>& meetings)
{
  std::cout << "\n";
  for (auto& meeting : meetings)
  {
    std::cout << "( " << meeting.startTime << ", " << meeting.endTime << " )" << "\n";
  }
  std::cout << "\n";
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接