简体   繁体   中英

sorting elements of a vector containing objects

I have class and this class contains a number. And I have a vector contains object pointer of class. And I want to sort that objects according to their numbers. How can I do this? Thanks for answers.

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Course
{
public:
    Course (int code, string const& name) : name(n), code(c) {}
    int getCourseCode() const { return code; }
    string const& getName() const { return name; }

private:
    string name;
    int code;
};

int main()
{
    vector<Course*> cor;
    vector<Course*>::iterator itcor;

    cor.push_back(new Course(3,"first"));
    cor.push_back(new Course(2,"sekond"));
    cor.push_back(new Course(4,"third"));
    cor.push_back(new Course(1,"fourth"));
    cor.push_back(new Course(5,"fifth"));  
    sort (cor.begin(), cor.end());
    for (itcor=cor.begin(); itcor!=cor.end(); ++itcor) {
        cout << *itcor << ' ';
    }
}

For example when I want the sort the objects they are being sorted according to their adresses.

You'll need to provide a custom comparator class or function to the std::sort method to make it not sort by addresses.

template <class RandomAccessIterator, class Compare>
void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );

where comp can be defined as:

bool comparer(const Course* x, const Course* y) { /*compare*/ }
//or
struct comparer {
  bool operator() (const Course* x, const Course* y) { /*compare*/ }
} comparerObject;

and call sort as:

std::sort(cor.begin(), cor.end(), comparer);   //method alternative

or

std::sort(cor.begin(), cor.end(), comparerObject);   //class alternative

That, or don't keep pointers in the vector . From the code you posted, it's not clear that you actually need pointers:

vector<Course> cor;

should be enough.

You can do this in three ways:

1) Overload the < operator, and call std::sort algorithm. The code will look like this:

bool operator<(Course *a, Course *b) const {
  // do comparison
  return A_BOOL_VALUE;
}

std::sort(array_of_courses.begin(),array_of_courses.end());

The first way is wrong, as you can't overload the < operator in pointers.

2) Create a compare function, and then call the second version of std::sort . The code looks like this:

bool compare(Course *a,Course *b) {
  // do comparison
  return A_BOOL_VALUE;
}

std::sort(array_of_courses.begin(),array_of_courses.end(),compare);

3) Create a compare class, which has it's () operator overloaded, and then call the third verion of std::sort . The code:

struct Compare {
  bool operator()(Course *a, Course *b) {
    // do comparison
    return A_BOOL_VALUE;
  }
};

std::sort(array_of_courses.begin(),array_of_courses.end(),Compare);

Note: the sort function is found at the algorithm header file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM